Gridview获取值文本框

时间:2014-06-23 15:00:22

标签: c# asp.net gridview textbox

以这种方式在gridview中动态创建文本框:

int index = e.Row.RowIndex;
int tot = e.Row.Cells.Count;
for (int i = 1; i < tot; i++)
{
    TextBox txtValor = new TextBox();

    txtValor.Width = 15;
    txtValor.BorderStyle = BorderStyle.Ridge;
    txtValor.ID = "txt"+index.ToString()+i.ToString();
    txtValor.Attributes.Add("runat", "server"); 
    txtValor.Text = (e.Row.DataItem as DataRowView).Row[produ].ToString();
    e.Row.Cells[i].Controls.Add(txtValor);              
}

我无法获得价值,已经尝试过以下方式:

quant = GridView1.Rows[i].Cells[j].Text;
quant = ((TextBox)(GridView1.Rows[i].Cells[j].Controls[0])).Text;
quant = ((TextBox)GridView1.Rows[i].Cells[j].FindControl("txt"+i.ToString()+j.ToString())).Text;
quant = GridView1.Rows[i].Cells[j].Text; // this way i get the value of the cell and not the textbox

Tgis是代码隐藏的ASPX:文本框是由行数据绑定创建的。

   <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Size="XX-Small" ForeColor="#333333" Width="100%" HorizontalAlign="Center" PageSize="35" OnRowDataBound="GridView1_RowDataBound">
        <AlternatingRowStyle BackColor="White" ForeColor="#6E7265" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#BDC0C4" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="White" ForeColor="#333333" Wrap="True" BorderStyle="Double" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" ForeColor="White" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" /
    </asp:GridView>

2 个答案:

答案 0 :(得分:0)

C#有一个方法可以让你通过ID找到一个控件:

private void Button1_Click(object sender, EventArgs MyEventArgs)
{
      // Find control on page.
      Control myControl1 = FindControl("TextBox2");
      if(myControl1!=null)
      {
         // Get control's parent.
         Control myControl2 = myControl1.Parent;
         Response.Write("Parent of the text box is : " + myControl2.ID);
      }
      else
      {
         Response.Write("Control not found");
      }
}

获得控件后,将其转换为文本框控件,然后只获取文本属性:

string txtValue = ((TextBox)myControl1).Text;

编辑:Dimitri E提出了一个很好的观点,你应该找到一种方法来为控件分配唯一的ID,以使这种方法可靠地工作

参考链接:http://msdn.microsoft.com/en-us/library/486wc64h(v=vs.110).aspx

答案 1 :(得分:0)

txtValor.ID = "txt"; 

将其更改为:

txtValor.ID = "txt" + i.ToString(); 

然后您将能够在以后使用您的代码找到它,否则您将创建一堆具有相同ID的文本框:“txt”

for (int i = 0; i < 3; i++)
{
    TextBox txt =  (TextBox)FindControl("txt" + i.ToString());
    MessageBox.Show(txt == null ? "Not Found!" : txt.Text);
}

此代码将遍历名为txt0,txt1,txt2的文本框,将尝试查找它们并显示带消息的消息框。如果找到文本框,则会显示值,否则“未找到!”将会呈现。您可以使用不同的内容替换消息框,或者只将其输出到标签。

protected void Page_Load(...
{
    // 1. Get data from DB if Session variable is null or data have changed
    // 2. Save it to Session Variable
    // 3. Create Controls using a session variable (this one will 
    //       be called on each post-back)
    // 4. Click event is called after Page_load completes. 
    //       Your textboxes are in place to be found.
    //      
}

希望这有帮助。