2013-01-10 110 views
1

我在页面aspx中有gridview控件和textbox +按钮控件。将文本框值传递给gridview

我想通过单击按钮将文本框的值传递给gridview。

<asp:GridView ID="gvName" runat="server" ViewStateMode="Enabled"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
<asp:Label ID="lblName" runat="server"></asp:Label> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView> 

,并使用此代码:

protected void btnAddName_Click(object sender, ImageClickEventArgs e) 
    { 
     foreach (GridViewRow row in gvName.Rows) 
     { 
      if ((Label)row.FindControl("lblName") is Label) 
      { 
       ((Label)row.FindControl("lblName")).Text = txtName.Text; 
      } 
     } 
    } 

但它没有确定。 :-(请帮我

+0

你是什么目的呢? –

+0

什么是你所面临的问题你可以解释一下吗? – MahaSwetha

+0

只是你想分配的texbox值在gridview标签?你需要什么? – Nag

回答

1

如果网格行已与id lblName标签,那么你将排得到它,你不必再检查一遍

foreach (GridViewRow row in gvName.Rows) 
{ 
    Label lblName = (Label)row.FindControl("lblName");   
    lblName.Text = txtName.Text;    
} 

注意:如果你这样做没有行已经在THR GridView控件,那么你将无法,您需要确保在电网其ARE行(S)获得标签的值。

+0

我在点击按钮事件中使用此代码,但不起作用。不向gridview插入值。 –

+0

你绑定了你的网格视图吗?网格中有多少行? – Adil

+0

我插入文本框中的值,然后单击但不插入值textboxt到gridview –

3
Create a rowdatabound command and place the below code. 

protected void gvName_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      Label lblName = (Label)row.FindControl("lblName");   
      lblName.Text = txtName.Text; 
     } 
} 
+0

我使用这个代码但是没有在gridview中插入值(gridview为空) –

+0

首先,绑定gridview和数据源并尝试这个。 – MahaSwetha

相关问题