【问题标题】:Gridview Get Value TextboxGridview 获取值文本框
【发布时间】:2014-06-23 15:00:21
【问题描述】:

以这种方式在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>

【问题讨论】:

  • 你能显示你的 ASPX gridView 标记吗?另外,您在哪种方法中创建新的 TextBox?

标签: c# asp.net gridview textbox


【解决方案1】:

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");
      }
}

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

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

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

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

【讨论】:

    【解决方案2】:
    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.
        //      
    }
    

    希望这会有所帮助。

    【讨论】:

    • 好点,虽然它没有告诉他如何实际找到控件
    • 另一件要考虑的事情:在尝试从中读取值之前,您必须在每次回发时重新创建控件。否则你会在这一行得到一个空值:TextBox txt = (TextBox)FindControl("txt" + i.ToString());
    • 是的,但是有些东西正在将数据加载到 GridView1,对吧?
    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多