I have a LinkButton(LinkButton_x) in my aspx page. i'm using this LinkButton to check all boxes in CheckBoxList. when i clicked on this LinkButton(LinkButton_x), code in partial class is executing to select all the boxes in CheckBoxList.
also have several LinkButtons in a TemplateField of a GridView as below.
<asp:TemplateField HeaderText="">
<ItemTemplate>
LinkButton1 here
LinkButton2 here
</ItemTemplate>
<EditItemTemplate>
LinkButton3 here
LinkButton4 here
</EditItemTemplate>
<FooterTemplate>
LinkButton5 here
LinkButton6 here
</FooterTemplate>
</asp:TemplateField>
LinkButtons in TemplateFields FooterTemplate is normally displaying. but When i click on LinkButton_x in aspx page, LinkButtons in TemplateFields ItemTemplate also displaying. i want to avoid it.
how can i solve this ?
If you have Linkbutton in <ItemTemplate> and you don't have written any logic to hide the linkbutton, then it will obviously display it provided that grid contains atleast one row.
I think your problem is more related to binding of grid and its number of rows during postback. It may be the case that when you click on linkbutton then due to postback grid would be getting change.
Related
I have a gridview that contain label, the value of this label is assigned in code behind,
I enabled sorting in my gridview and when I click the header to sort the value of the label disappear but the other DataFields doesnt.
<asp:TemplateField HeaderText="<%$Resources:mj.resource, req_category%>" SortExpression="category_id">
<ItemTemplate>
<asp:Label ID="category_id" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Have you set EnableViewState="false" on the GridView or at the page level?
Are you using DataSourceControl (like SqlDataSource) to bind data to the GridView?
If so, you'd have to manually sort and bind data again in the Sorting event.
I have a grid view in a asp page which have a hyperlink field,say,CustomerID which navigates user to various webpages. The value of the CustomerID in the grid view can have duplicate values. So, what I want is how to record all the clicked values in the CustomerID or the hyperlink?
Please suggest solution such that the solution is viable for multiple hyperlink column in gridview each navigation to different webpage.
You can use combination of gridview bind columns and pass them as parameters in url and retrieve in other page as Request.QueryString[] collection.
Eg
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" NavigateUrl='<%# string.Format("~/Details.aspx?CustomerID={0}&Name={1}&Country={2}",
HttpUtility.UrlEncode(Eval("CustomerID").ToString()), HttpUtility.UrlEncode(Eval("Name").ToString()), HttpUtility.UrlEncode(Eval("Country").ToString())) %>'
Text="View Details" />
</ItemTemplate>
</asp:TemplateField>
I have a TextBox in a GridView which takes a user input. The problem is that it loses its value when I click on a Button placed in the GridView footer which adds an empty row to the GridView,How can I fix it?
Also, this happens only when I set its Disabled attribute to false in RowDataBound event. But if I set the ReadOnly attribute, everything works fine.
This is the TextBox in GridView.Bound to a DB data:
<asp:TemplateField HeaderText="Shift Code">
<ItemTemplate>
<asp:TextBox runat="server" CssClass="txtingrid" ID="lblShiftCode" Text='<%# Eval("SHIFTCODE") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
this is what I do in the RowDataBound:
if (e.Row.RowType == DataControlRowType.DataRow)
{
lblShiftCode.Attributes.Add("disabled", "true");
}
Please help.:)
lblShiftCode.Attributes.Add("disabled", "disabled");
If you are doing a post back on the button click, you will have to set the UseSubmitBehavior attribute to "false" on the button to retain values of the disabled controls.
More info here
Its very simple just rebind the gridview after clicking the button. Suppose you are using gridbind(); method to bind your gridview.So you have to call this method again after button click.
I've created a gridview with edit feature. On clicking the gridview row edit image, it makes the custom edit panel visible and the user can edit and save the gridview row data or s/he can click the cancel button without any updates. It works perfectly fine with .NET framwork 2.0.
But, the problem lies with the .NET framework 4.5 -
when the user clicks the cancel or save button, instead of loading the
normal gridview, it loads the default edit template on the top of
gridview row where the user had clicked before.
So, how can I solve this problem? This happens in all the Gridviews used in our ASP.NET website.
Here's the original Gridview Row: Original GridRow
Here's the custom edit panel with save/cancel: Custom Edit Panel
Here's error: Default Template loaded
I usually achieve what you are trying to do in the row databound event.
Try setting up your grid view like this
<asp:Gridview id="ClientManagementGridView runat="server"/>
<columns>
<asp:TemplateField>
<itemtemplate>
//put edit button here
</itemtemplate>
<edititemtemplate>
// put your update and cancel buttons here
</edititemtemplate>
</asp:Templatefield>
<asp:templatefield>
<itemtemplate>
<aspTextBox Text='<#Eval("ClientId") %>' runat="server" />
</itemtemplate>
<edititemtemplate>
<asp:Panel id="EditPanel">
//your panel contents
</asp:Panel>
</edititemtemplate>
</asp:templateField>
<asp:BoundField DataField="ClientName" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Name" />
</columns>
<asp:GridView>
Now in your row data bound event of your gridview put this magic piece of code
If e.Row.RowState.ToString().Contains("Edit") Then
Dim editGrid As GridView = TryCast(sender, GridView)
Dim colSpan As Integer = editGrid.Columns.Count
e.Row.Cells(0).Visible = True
e.Row.Cells(1).Visible = True
For i As Integer = 2 To colSpan - 1
e.Row.Cells(i).Visible = False
e.Row.Cells(i).Controls.Clear()
Next
e.Row.Cells(1).Attributes("ColSpan") = (colSpan).ToString()
End if
As you can see I am overriding the default edit item template by clearing its default controls and then changing the column span of the cell to fit my custom edit panel.As well as hiding cells I want to be hidden.
This a pretty simple question, I'm just not sure how to do it exactly. I would like to bind a Button or perhaps ImageButton to a GridView in ASP.NET/C#. Currently, the GridView has two columns and is bound to a DataTable with two columns. I want to add a third column to the GridView, which will include the Button.
I know GridView has ButtonField, but I'm not too sure how to go about using it to do what I want. I want to dynamically generate these Buttons and add them to the GridView.
Here is how my GridView looks right now:
<asp:GridView
ID="GridView1"
Runat="server">
<Columns>
<asp:HyperLinkField
HeaderText="Display Name"
DataNavigateUrlFields="DISPNAME"
DataNavigateUrlFormatString="ViewItem.aspx"
DataTextField="DISPNAME">
<ItemStyle Width="70%" />
</asp:HyperLinkField>
<asp:BoundField
DataField="TypeDisp"
HeaderText="Type">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
You can use a template field like the following,
<TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="image url" CommandName="SomeCommand" CommandArgument='<%# Eval("Id") %>'/>
</ItemTemplate>
</TemplateField>
Then you can handle the RowCommand event of the GridView and check the e.CommandName to see what command to be executed and you can get the e.CommandArgument as well which could be the row Id like I used in the code above.
If we are talking a button that's always present, you can use ButtonField, or even use a TemplateField and provide the template with the button, and bind the data to the button (sounds like you may want to bind data to the attributes of the button?)
If you are looking to dynamically generate buttons in the UI, tap into the RowCreated event and add the button the GridView. You'd have to do this on every page load; the GridView won't remember a button created programmatically.
HTH.