繁体   English   中英

单击其他单元格后,如何在文本框中获取gridview的单元格值?

[英]How to get the cell value of a gridview in text box when other cell is clicked?

我有一个带有4列的gridview,即:“用户ID”,“描述”,“显示密码”,“更改密码”。

我在每个单元格的“更改密码”列中都放置了一个“图像”按钮,并且在Gridview下方有3个文本框。

当我单击更改密码时,该特定行的用户标识应显示在文本框1中。

如何在C#和后端是MySql DB服务器的asp.net中执行此操作?

我的偏好是使用CommandArgument属性,并将click事件的方法直接分配给后面的aspx代码中的按钮:

<asp:Button ID="btnChangePassword" CommandArgument='<%# Eval('UserID') %>' OnClick="MyEventMethod" />

然后,在后面的代码中,您将有一个处理它的方法:

public void MyEventMethod(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    this.TextBox1.Text = btn.CommandArgument;
}

另一种方法是使用JavaScript(避免回发)和onClientClick属性。 在gridview上方的某个位置,您将找到一种方法来处理此分配。

<script type="text/javascript">
    // Gets the actual reference to the textbox in javascript
    // using the client id
    function SetTextBoxUserID(userid)
    {
        var textbox1 = document.getElementById('<%= this.TextBox1.ClientID %>');
        textbox1.value = userid;
    }
</script>

然后该控件将如下所示:

<asp:Button ID="btnChangePassword" OnClientClick='SetTextBoxUserID(<%# "'" + Eval("UserID") + "'" %>)' />

将“更改密码”列更改为“超链接”字段。在“数据”部分中该列的属性中,您可以看到DataNavigateUrlFields属性。 将其设置为“用户ID”。 然后在DataNavigateUrlFormatString属性中设置为同一页面,如下所示

~/User.aspx?UserId={0}

然后在页面加载事件中编写以下代码

String userId= Request["UserId"];
        if (!string.IsNullOrEmpty(userId))
            TextBox1.Text = userId;

这可能不是最好的拍摄方法,但这可以解决一些问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM