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

时间:2015-12-25 17:20:47

标签: c# asp.net gridview

我的文本框的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>

当用户点击“Generete new Password”时,它会转到此功能,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(),但它仍然没有工作。

请帮助:)

谢谢!

1 个答案:

答案 0 :(得分:0)

这是正确的方法:

<?
相关问题