Skip to content Skip to sidebar Skip to footer

Add Value To TextBox In Nested GridView By Using JQuery

I have a Nested Gridview having a link is used to show and hide div tag in nested gridview. By default this Div Tag is disable when we click on a link to show(enable) Div tag unde

Solution 1:

I don't know much about ASP, but if this is rendering the textbox in the webpage similar to <input type='text' /> then you want to use $("#TextBox1").val('@username') rather than .text()


Solution 2:

The solution is quite similar to the one I gave to your previous question. You can pass a reference to the anchor tag in the call to showDiv (using the this keyword) and set the class name of the div (e.g. innerDiv):

<a href='#' onclick='showDiv(this); return false;'>ShowDiv</a>
<div id='divShow' class='innerDiv'>

The showDiv could find the table cell container, then the element with the innerDiv class name and the text box.

function showDiv(lnk) {
    var $div = $(lnk).closest('td').find('.innerDiv');
    $div.show();
    $div.find('input[type=text]').val('@username');
}

Please note that I use class names and element types in the jQuery selectors. The ID of the elements are not reliable since each row of the GridView displays these controls, which should all have a different ID.


Solution 3:

You have two issues with your code.

First, ASP.NET generates complex IDs for server controls (something like blabla_gridparent_gridchild_textbox1_blabla, instead of simple textbox1), so if you want to work with you control on client side, you have to ask ASP.NET for that ID via ClientID property of your server control:

function showDiv() {            
     $("#divShow").show();
     $("#<%= TextBox1.ClientID %>").val('@username');
     return false;
}

Note, this is a server code. I assume this JS code placed into your .aspx page (or .ascx control), not in static .js file. In case of static .js file you need to pass that ID as parameter.

Second, you have to use .val() jquery method to work with input value. TextBox is rendered into input.


Post a Comment for "Add Value To TextBox In Nested GridView By Using JQuery"