Asked by:
Label Value

Question
-
User-797751191 posted
Hi
Label Value not getting displayed when i assign in rowcommand event in gridview
<asp:Label ID="lbl1" runat="server" Text="Label" ></asp:Label>
<asp:Label ID="lbl2" runat="server" Text="Label"></asp:Label>Thanks
Wednesday, February 6, 2019 10:38 AM
All replies
-
User753101303 posted
Hi,
You tried https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2017 to see how your code runs ?
Check that this event happens. If testing a command name make sure you enter the if branch in which you change this label. You could also start with something dead simple for testing such as :
void MyGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { lbl1.Text="Should work?"; }
You don't debug code by wondering what happens. You are using tools to see how your code runs and possibly conduct some experiments on your code to test ideas and better understand how it behaves and find out what is wrong...
F12 Elements or "view source" can also help to inspect the markup (for example in case the label is changed but is currently inside a display:none element).
Wednesday, February 6, 2019 11:54 AM -
User-1174608757 posted
Hi jsshivalik,
According to your description, Could you tell me where the label is ? If it is in Gridview, you couldn't directly assign its value by id in code behind since it is in the Gridview. If it is outside the Gridview you could set the value by id. Here are two demos ,I hope it could help you.
label outside Gridview:
Girdview.aspx:
<head runat="server"> <title></title> <script src="../Scripts/jquery-3.3.1.js"></script> </head> <body> <form id="form1" runat="server"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="249px" Width="336px" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:BoundField DataField="IDnumber" /> <asp:BoundField DataField="Name" /> <asp:BoundField DataField="English" /> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Button" CommandName="get" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
code behind:
public partial class Gridview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql = "select * from students"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "get") { Label1.Text = "lala"; } } } }
it shows as below:
When label is in Gridview, you should use findcontrol method to get the label . Here is the demo, I hope it could help you.
aspx:
<head runat="server"> <title></title> <script src="../Scripts/jquery-3.3.1.js"></script> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="249px" Width="336px" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:BoundField DataField="IDnumber" /> <asp:BoundField DataField="Name" /> <asp:BoundField DataField="English" /> <asp:TemplateField> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Button" CommandName="get" CommandArgument="<%# Container.DataItemIndex %>" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
in code behind:
public partial class Gridview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql = "select * from students"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "get") { int index = Convert.ToInt32(e.CommandArgument);// get row index which you select GridViewRow row = GridView1.Rows[index];// get the row Label label = row.FindControl("Label1") as Label; label.Text = "defined in Gridview"; } } }
it shows as below:
Best Regards
Wei Zhang
Thursday, February 7, 2019 3:38 AM