Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
I'm having trouble retrieving values for controls inside a gridview using javascript. An example below.

JavaScript
function percentIT() {
        var DropDown = document.getElementById('DropDownList2');
        alert(DropDown);
    }


DropDownList2 is obviously the name of the control and I have checked this.

I've seen code like the following but it does not seem to work with me.

JavaScript
function percentIT() {
        var DropDown = document.getElementById('DropDownList2').value;
        alert(DropDown);
    }


I'm new to client side code and need some help

Thanks
Posted

Try using the GridView1_RowDataBound event.
Once the event fires, for each row (passed as event parameter), locate the dropdown.
Something like e.Row.FindControl("DropDownList1");
 
Share this answer
 
Comments
Andrew Chambers 7-Feb-12 17:23pm    
I'm looking for the client side solution not the server side.
ASP.NET will manage IDs by adding prefixes for the container the control is in. DropDownList2 may be what you have assigned but when rendered it will be something like $ctrl00_DropDownList2. View the rendered source of your page and you will see this.

Using JQuery you would do this, which will find the control no matter what the prefix is.
JavaScript
$("[id$='DropDownList2']")

You should learn to use JQuery rather then relying on getElementById. It will make your client-side life much easier.
 
Share this answer
 
Comments
Andrew Chambers 7-Feb-12 17:21pm    
Im looking to find the value of the control. Not the control itself.
[no name] 7-Feb-12 17:25pm    
And how are you going to find the value without first finding the control??
Andrew Chambers 7-Feb-12 17:36pm    
Sorry mark. My apologies. I was under the impression however wrong that
var DropDown = document.getElementById('DropDownList2');
will find the control.
Is this not correct?
[no name] 7-Feb-12 17:49pm    
It will find an element with that specific id. The problem is, as I explained, ASP.NET will add prefixes to element ids based on the their location in the DOM when rendered. So DropDownList2 maybe what you set the id as in the markup, but when rendered on the client, where the JavaScript is run, it may not be the same name. That is where JQuery is better because of the flexibility in selecting DOM elements.
Andrew Chambers 7-Feb-12 18:05pm    
thanks for this explanation mark. i need to do some research on jquery. i have now found the rendered page and the correct id.
Hi,

Try using the GridView1_RowDataBound or GridView1_RowUpdating event.
Example:

C#
var dropDown = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2")).SelectedValue;



Please do not forget to vote if could help so that others may consider as an answer...
 
Share this answer
 
v2
Comments
Andrew Chambers 7-Feb-12 17:25pm    
this looks like a mixture of client side and server side code.
try this code

HTML
function percentIT() {

       var gridview = document.getElementById("<%= grdView.ClientID %>");
 
       if (gridview == null) return; 
       var statusCell = gridview.rows[parseInt(selectedRowIndex)+ 1].cells[5]; 
       var dropDown = statusCell.childNodes[1]; 
 
       var FdropDown= dropDown.options[dropDown.selectedIndex].value; 
            
}
 
Share this answer
 
v2
Comments
[no name] 7-Feb-12 7:57am    
Using <%= grdView.ClientID %> couples the JavaScript too tightly to the page for my tastes. JQuery, and other frameworks makes this unnecessary in most cases.
zyck 7-Feb-12 8:41am    
either you use javascript or jquery, it depends on how you solve it
the reason why I make <%= grdView.ClientID %> is because I dont know
the id of his gridview im assuming this solution is an example or reference only but if you want to enhance, feel free

Andrew Chambers 7-Feb-12 17:23pm    
selectedRowIndex is not recognized.
Where do you get ddlStatus?
zyck 7-Feb-12 17:59pm    
I Improved the solution you may try again the solution
that thing you want handled in client-side or server side.


i think you will used rowdatabound method
which is good for u

i used following code to get dropdownlist

DropDownList ddlStatus = (DropDownList)e.Row.Cells[2].FindControl("ddlStatus");

C#
string status = ddlStatus.SelectedItem.Text.ToString();


may be help this
Thank U
@ChetanV@
 
Share this answer
 
v3
Comments
Andrew Chambers 7-Feb-12 17:24pm    
if you looked at the code and the question you will see its client side.
Hi, use below code in JS

JavaScript
function percentIT() {
            var DropDown = document.getElementById('<%=DropDownList2.ClientID%>');
var valDDL = DropDown.options[DropDown.selectedIndex].value; 
            alert(valDDL);
        }
 
Share this answer
 
Comments
Andrew Chambers 7-Feb-12 17:25pm    
'<%=DropDownList2.ClientID%>' is not recognized.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900