Reputation: 53
I have a GridView that bound some data from DB and have a label in the ItemTemplate and EditTextbox in the EditItemTemplate they are in the same TemplateField. In the GridView some user has no data in somewhere field, If I want to insert a data for these user, I need to find GirdView label first and var the edittextbox, when updating I can Compare the value of them , such as the edittextbox value not equals to labeldata value then insert, but I can't find the value of the label when rowupdating or databound of gridview
how can i do it ?
I have try
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int rowCount = GridView1.Rows.Count;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (rowCount >= 1)
{
Label lbDA_TEL_HK_NO = ((Label)e.Row.FindControl("lblKM_TEL"));
Session["DA_TEL_HK_NO"] = lbDA_TEL_HK_NO.Text;
}
}
}
It can find all the gridview data but not which I selected
P.S I'm a newbie, please help me
<asp:TemplateField ItemStyle-Width = "150px" HeaderText = "香港內線">
<ItemTemplate>
<asp:Label ID="lblHK_TEL" runat="server"
Text='<%# Eval("[DA_TEL_HK_NO]")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtHK_TEL" runat="server" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" MaxLength="3"
Text='<%# Eval("[DA_TEL_HK_NO]")%>'></asp:TextBox>
</EditItemTemplate>
Upvotes: 2
Views: 3520
Reputation: 3228
You can try something like this for read all rows in grid view:
for (var i = 0; i < GridView1.Rows.Count; i++)
{
var label = GridView1.Rows[i].FindControl("lblKM_TEL") as Label;
if (label != null)
{
// Manipulate label control
}
}
Or you can gets label from selected row:
var label = GridView1.SelectedRow.FindControl("lblKM_TEL") as Label;
if (label != null)
{
// Manipulate label control
}
Upvotes: 1
Reputation: 8271
From your aspx markup you are using the wrong label id .
you need yo use this label id lblHK_TEL
You code looks as follows after changes
Label lbDA_TEL_HK_NO = ((Label)e.Row.FindControl("lblHK_TEL"));
Upvotes: 1