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 ->
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString))  but I don't know the syntax.
 
 
  
0
abev
4/29/2009 6:30:30 PM
asp.net.client-side 24353 articles. 0 followers. Follow

3 Replies
1938 Views

Similar Articles

[PageSpeed] 14

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.
0
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.

0
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:
  
<%@ 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
Lance
5/5/2009 5:46:55 AM
Reply:

Similar Artilces:

textbox value set on client side, is not visible on server side or, on the source of the page.
hi, I have a textbox on my webform. the first time I load my page I fill this textbox with  "name" on the server side. once the page is loaded I set the value of the textbox to "thisIsme" with client side code. I even see a different text in the texbox. client side:document.all('txtnameInput').value =  'thisIsme'; server side:Me.txtnaam.Text 'is stil "name" and not "thisIsme" Why is this happening? on the webpage I see the text "thisIsme", but when I look at the source of the webpage I still see the old value in it. it's very odd... I am NOT setting the value back to "name...

Client side click on gridview to confirm delete
I have a gridview with a delete button as follows...<ItemTemplate>                <asp:ImageButton id="deleteAttachment" OnClientClick='return                    confirm("Are you sure you want to delete this Attachment?");'                    imageallign="absbottom" commandName="DeleteAttachment" tooltip="Delete this        &nb...

Client-side handle Select click on GridView
Is it possible to put JavaScript into the OnClick event of the automatically generated Select links that the GridView will generate for you?...

Client side : Click one of 4 check box list will set read only true on a text box
Halo All   I have a Checkboxlist with 4 item inside, and a textbox If user click item no 2, i want the textbox to be set readonly false if item no 2 is uncheck, i want the textbox to be cleared and set readonly true   But my code will always think that the item no 2 is checked. What did i miss on the Javascript?    This is my Javascript :  function CheckboxClick() (       if (document.getElementById("chkAction_1").value == 'on')       {        &nb...

pass client textbox value to a server side textbox
HiDoes anyone know how in javascript to pass a client side textbox value to a server side textbox?Thanks in advance.Graham. An asp textbox is a resular textbox. so you do it the same way.document.formName.elementName.value = document.formName.elementName2.value;One thing to note is that the asp textbox is going to have a slightly different name then what you named it.EricPascarello.com | Twitter epascarello | LinkedIn...

Jscript, moving from client-side to server-side (.NET)
Hey all I’m working translating a web-app written in client-side Jscript to run on the server-side as JScript.NET. Without needing to get too specific, I am using Jscript because the original page aggregated raw data to produce statistical results (this was done dynamically based on user 'sort-by' options). The final output is an html report that can be emailed. Because this is currently being done on the client side, as the raw data grows the load-time grows as well (to the point that has almost become unusable). I’m posting here to see if anyone has any experi...

Data format in TextBox in both Server side and Client side?
HI,      I have a GridView which shows data in TextBox.      I was wondering if there is a way to do data format (Currency) like $12,203.00. I need it working in server side (for existing data) and client side (for new data or changed data). What I mean about client side is data automatically formatted after data is keyed in.     Thanks for your help. Hi you can use MaskEdit in AjaxControlToolkit please check here http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx this is a cool control. Hope it...

repeater with textboxes. how to read the textbox values on client side
Hi  I have a data bounded repeater with text boxes attached. How can I read the values of the textboxes using javascript?? Could not find a proper link.. Appreciate any solution , thanks     The repeater will add a prefix to each textboxes ID to ensure uniqueness.  The following function will loop through all textboxes of the repeater, and if the ID of the given textbox contains the string specified, the it will consider it a match and you can access it's value property easily.  Here is the function: function textboxFinder() { var repeater =...

Validation Controls in .NET ( Client side or server side?)
 i have a very basic question. We have all these validation controls in asp.net ( required field, range validator and others)..my question is the required field validator is a server side control. So does that mean that it does a post back?..is it client side or server side control  The only controls that post back are buttons and other inputs where you set AutoPostback="True", such as a dropdownlist or a textbox.  Another way of posting back is using an UpdatePanel and placing a trigger in it for a specified control and/or event. All controls are considered clien...

How to set a cookie in client side a acces that in server side?
Hi,I have used  [document.cookie = 'nav=another test; expires=Fri, 3 Aug 2009 20:47:11 UTC; path=/' ] the code enclosed in bracket to set the cookie in the client side. However if i try to access the same in the code file, the value comes as blank. Please guide me how to set it in client side and access in server side. ---------Drudge  To write -         HttpCookie cookie = Request.Cookies["Language"];        if (cookie == null)        {   ...

How can I set client side values as server side values?
Hi, Can anyone tell me how I can get client side values such as document.referrer, navigator.appName etc... into server side scripts? There are some client side values I'd like to set as session vars and insert into the db? How do I go about this? Thanks for any pointers Davlon PS (I'm using powersite 7.1.0.300) Pass them as page parameters, e.g., http://myserver/myurl?document.referrer=whatever&navigator.appName=yadayada Then, in your server-side script, just loop through the document.value array and get these values. -- pbm_hopethishelps, Roy Kie...

passing data to a server side textbox from client side script
Qu: passing data to a server side textbox from client side java script.Does anyone know how to do this?Thanks,Graham. Remember that when you view an aspx page in your browser, you are viewing straight html code (view the source).  Thus, access the textbox as you would a normal <input type="text> tag.HTH, rchern.Churned Ajax ToolkitPlease choose "Mark as Answer" for the posts that lead you to a solution....

Calling server side sub from client side button click
Hi, I have some javascript in my asp.net page, and a function within that, which is run when the client side form is submitted. What I need is for that function to run, which updates a server side variable, then following that a server side sub should run, using that (newly updated) variable. I have currently set it up with the  onsubmit= tag of the client side form running the first (JavaScript) function, and then the submit button at the bottom of the client side form calling the server side sub with its onClick property. The (probably quite daft!) thinking behind that was that the on...

gridview editing-(client side and server side operations)which is better?
Hi all, I have a doubt in editing from an gridview.. I have implemented two ways 1. using  html input button and javascript...(not used update panel) 2. at serverside using c#,using rowindex of gridview..(i ve used update panel)  which operation is faster among these... and my question is, there is many ways at server side , but why many of them choosing javascript for these type of operations.. what is security over these client side and server side operations..which one is the better way to follow...   thanks in advance, rajesh   If you have tested y...

Web resources about - Setting Textbox from gridview click - asp.net.client-side

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 1/19/2016 5:09:53 PM