Disable a TextBox in GridView and retain its value c# - c#

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.

Related

GridView Button reference on rowdatabound

I feel like I'm missing something obvious here but I'm just not getting it. I have a gridview with a template field column where i have a button and a hidden field. I'm trying to get the reference to both the button and the hidden field on row databound, as both the button's label and commandargument etc change depending on the other row data.
If i place a breakpoint in the area indicated in the code below, I can see that the hidden field is being assigned to properly, but the button is not. What am I missing here?
GridView.aspx
<asp:GridView ID="gvCurrentQueueStatus" runat="server" AutoGenerateColumns="False"
OnRowDataBound="gvCurrentQueueStatus_RowDataBound">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Service Controls">
<ItemTemplate>
<asp:Button ID="btnSubmitCommand" runat="server" Text="Control" OnClick="btnSubmitCommand_Click" />
<asp:HiddenField ID="hdnQueueNumber" runat="server" Value='<%# Eval("ReportQueueNumber") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView.aspx.cs
protected void gvCurrentQueueStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button controlButton = (Button)e.Row.FindControl("btnSubmitCommand"); // FindControl fails
HiddenField hdnQueueNumber = (HiddenField)e.Row.FindControl("hdnQueueNumber"); // FindControl succeeds
// Other stuff
} // breakpoint here, successfully finds htnQueueNumber, but not btnSubmitCommand
}
Try this:
Button controlButton = e.Row.FindControl("btnSubmitCommand") as Button ;
Or try this way:
foreach (GridViewRow row in gvCurrentQueueStatus.Rows) {
Button controlButton = (Button)gvCurrentQueueStatus.Rows[row.RowIndex].FindControl("btnSubmitCommand");
}
So apparently my button was in fact getting properly assigned to, it was just not reporting as such when mousing over the variable in my debugger.
I'm not actually crazy, Visual Studio's debugger is however and needed the old "have you tried turning it off and on again".
Sorry about that those that helped look into my issue :3

.NET framework 4.5 loads default edit template on GridView?

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.

LinkButton display at unnecessary time

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.

Get value from GridView when a button is clicked

I have a GridView , and the GridView has an item template. Now I need to get the value of the first cell which contains the value I need but I have tried the following and does not work, it eturns a ""
int id = Convert.ToInt32(GridView.Rows[index].Cells[0].Text);
Here is the code I have in the gridview
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label runat="server" ID="lblDepartmentID" Text='<%#DataBinder.Eval(Container.DataItem,"DepartmentID")%>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
I have to emphazise that I do not want to use the GridView_RowCommand or other GridView events.. I need to pull this value upon clicking a button on the same page.
How can I do this?
If you are trying to access the text value of Label then you have to first get the reference to Label control like this:
Label lbl = (Label)GridView.Rows[index].FindControl("lblDepartmentID");
and then use it's Text property to get the value required:
int id = Convert.ToInt32(lbl.Text);

Binding a Button to a GridView

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.

Categories