访问gridview中的文本框并设置其值

本文关键字:设置 文本 gridview 访问 | 更新日期: 2023-09-27 18:27:59

我有一个ID为txtPassword的文本框。

<FooterTemplate>
           <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
           <asp:Button ID="PasswordGenerator" runat="server" Text="Generete new Password" OnClick="PasswordGenerator_Click" />
</FooterTemplate>

当用户点击"生成新密码"时,它进入该功能,txtPassword文本框将自动填充生成的密码。

protected void PasswordGenerator_Click(object sender, EventArgs e)
{
    int length = 6;
    const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    StringBuilder res = new StringBuilder();
    Random rnd = new Random();
    while (0 < length--)
    {
        res.Append(valid[rnd.Next(valid.Length)]);
    }
    ((TextBox)GridView1.FindControl("txtPassword")).Text = res.ToString();
}

但是我无法从C#代码访问它。所以我搜索了一下,发现我需要使用findcontrol(),但它仍然不起作用。

请帮忙:)

谢谢!

访问gridview中的文本框并设置其值

以下是正确的方法:

TextBox Password = GridView1.FooterRow.FindControl("txtPassword") as TextBox;
if (Password != null)
    Password.Text = "Hello";