Setting Textbox from gridview click


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 If

            
 

            
So 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 ->

            
<i>e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString))</i>  but I don't know the syntax.

            
 

            
 
  
-1
abev
4/29/2009 6:30:30 PM
📁 asp.net.client-side
📃 24353 articles.
⭐ 2 followers.

💬 3 Replies
👁️‍🗨️ 3749 Views



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 Sub

            
Otherwise if it is not in a control just assign it to me.textbox1.text
 

View Brenden Kehren's profile on LinkedIn
Remember to mark as answer if this post answered your question.
-1
b471code3
4/30/2009 1:12:43 AM

 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.
-1
abev
4/30/2009 5:15:02 PM
Hello abev

 
The facing needs can be achieve by JavaScript, please try the following demo, which works fine on our lab:
  
&lt;%@ Page Language=&quot;C#&quot; %&gt;

&lt;%@ Import Namespace=&quot;System.Data&quot; %&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

&lt;script runat=&quot;server&quot;&gt;

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(&quot;name&quot;, typeof(string));
        dt.Columns.Add(&quot;comment&quot;, typeof(string));

        DataRow nrow = dt.NewRow();
        nrow[0] = &quot;Lance&quot;;
        nrow[1] = &quot;Hello World&quot;;

        dt.Rows.Add(nrow);

        nrow = dt.NewRow();
        nrow[0] = &quot;Carol&quot;;
        nrow[1] = &quot;Hello Lance&quot;;

        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(&quot;onclick&quot;, &quot;comment(&#39;&quot;+e.Row.Cells[0].Text +&quot;&#39;);&quot;);
        }
    }
&lt;/script&gt;

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head runat=&quot;server&quot;&gt;
    &lt;title&gt;&lt;/title&gt;

    &lt;script type=&quot;text/javascript&quot;&gt;

        function comment(name) {
            document.getElementById(&quot;&lt;%=TextBox1.ClientID %&gt;&quot;).value = &quot;@&quot; + name;
        }
    
    &lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
    &lt;div&gt;
        &lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot; /&gt;
    &lt;/div&gt;
    &lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot; OnRowDataBound=&quot;GridView1_RowDataBound&quot;&gt;
    &lt;/asp:GridView&gt;
    &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Height=&quot;158px&quot; TextMode=&quot;MultiLine&quot; Width=&quot;345px&quot;&gt;&lt;/asp:TextBox&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
 
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.
-1
Lance
5/5/2009 5:46:55 AM
Reply: