I want to fill a textbox when the user clicks a row in the Gridview. I have it working but I took the very long way.
1 Protected Sub QuibGV_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles QuibGV.RowCommand 2 3 Try 4 5 6 If e.CommandName = "Select" Then 7 8 Dim myGV As GridView = sender 9 Dim UserNameLabel As HiddenField = CType(myGV.Rows(e.CommandArgument).FindControl("UserLink"), HiddenField) 10 11 Dim sUserName As String 12 sUserName = UserNameLabel.Value 13 14 Dim QuibTB As TextBox = CType(QuibTextUpdate.FindControl("QuibTextBox"), TextBox) 15 16 QuibTB.Text = "@" & sUserName 17 18 End If 19 20 Catch ex As Exception 21 22 Admin_LogFileOps.WriteTransElement("Error creating @Quib 200 " & ex.ToString, 1, "Error.txt") 23 24 End Try 25 26 End Sub
1 Protected Sub QuibGV_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles QuibGV.RowDataBound 2 3 If e.Row.RowType = DataControlRowType.DataRow Then 4 5 Try 6 7 If e.Row.RowType = DataControlRowType.DataRow Then 8 9 'e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FFF972'") 10 '// when mouse leaves the row, change the bg color to its original value 11 'e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;") 12 13 e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString)) 14 15 End If 16 17 Catch ex As Exception 18 19 Admin_LogFileOps.WriteTransElement("Error setting @Quib onclick quib 200 ", 1, "Error.txt") 20 21 End Try 22 23 24 End IfSo when a user clicks the gridview it fills a text box with a value from the clicked row. Some of you must find this funny how I took the very long road :) I am sure all I need is this ->e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString)) but I don't know the syntax.
![]() |
0 |
![]() |
If the TextBox is in a different control then you can do this
Protected Sub QuibGV_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles QuibGV.RowCommand Select Case e.CommandName Case "Select" Dim gv as GridView = CType(sender, GridView) Dim hidUser As HiddenField = CType(gv.SelectedRow.FindControl("UserLink"), HiddenField) Dim QuibTb As TextBox = CType(QuibTextUpdate.FindControl("QuibTextBox"), TextBox) QuibTb.Text = "@" & HidUser.Value.ToString() Edit Select End Select End SubOtherwise if it is not in a control just assign it to me.textbox1.text
Remember to mark as answer if this post answered your question.
![]() |
0 |
![]() |
b471 thank for the tidier code. It works but the function of it is not acting like I want it. Not b/c of your code, but maybe by design. Here's the whole proces: Users make comments that get displayed in a gridview. I a user wants to respond to another user he clicks the grid view row and '@user' is automatically inserted in to the comment textbox. The trouble is (I think) that the gridview is behind an update panel so when you click the gridview row there is a least a 2 to 3 second delay before the user name is inserted in to the textbox. It's almost like a postback - but I dont want that. I want the name to appear instantly client side.
![]() |
0 |
![]() |
Hello abev
The facing needs can be achieve by JavaScript, please try the following demo, which works fine on our lab:
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("comment", typeof(string)); DataRow nrow = dt.NewRow(); nrow[0] = "Lance"; nrow[1] = "Hello World"; dt.Rows.Add(nrow); nrow = dt.NewRow(); nrow[0] = "Carol"; nrow[1] = "Hello Lance"; dt.Rows.Add(nrow); this.GridView1.DataSource = dt; this.GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onclick", "comment('"+e.Row.Cells[0].Text +"');"); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function comment(name) { document.getElementById("<%=TextBox1.ClientID %>").value = "@" + name; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" /> </div> <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"> </asp:GridView> <asp:TextBox ID="TextBox1" runat="server" Height="158px" TextMode="MultiLine" Width="345px"></asp:TextBox> </form> </body> </html>
Thanks.
Lance Zhang
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |