Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to find the value of a property using javascript.

javascript function below:

<script type="text/javascript">
    function percentIT() {
        var gridview = document.getElementById("<%= GridView5.ClientID %>");
        var ddl = document.getElementById("DropDownList2");     

code behind...
protected void GridView5_RowDataBound(object sender, GridViewRowEventArgs e)

           if (e.Row.RowType == DataControlRowType.DataRow)
               Label lblRate = (Label)e.Row.FindControl("lblRate");
               Label lblTotal = (Label)e.Row.FindControl("lblTotal");
               DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList2");

           else if (e.Row.RowType == DataControlRowType.Footer)
               Label lblTotalAmount = (Label)e.Row.FindControl("lblTotalAmount");


The function is triggered by the onchange event of the dropdown list.

 <asp:GridView ID="GridView5" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" DataSourceID="AccessDataSource6" 
               <asp:TemplateField  Visible="True" ShowHeader="
               False" HeaderText="Agreement Options" HeaderStyle-HorizontalAlign="Left" FooterText="Payable monthly in advance (including Agreement Options):" ItemStyle-BackColor="#CCCCCC">
                        <asp:Label ID="lblItemDescription" runat="server" Width="382"
Text='<%# Eval("itemDescription") %>'></asp:Label>

<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                <asp:TemplateField >
                        <asp:Label ID="lblRate" runat="server" width="50" Text='<%# Eval("percentage") %>'  ></asp:Label>
                <asp:TemplateField >
                        <asp:DropDownList ID="DropDownList2" runat="server" Width="106" onchange="percentIT()"  >
                        <asp:ListItem Value="Yes"></asp:ListItem>
                        <asp:ListItem Value="No"></asp:ListItem>

                        <asp:Label ID="lblTotal" runat="Server"  Width="106"  />
                 <asp:Label ID="lblTotalAmount" runat="server"  />        
            <FooterStyle Font-Bold="True" />

I'm trying to find the value of the dropdown when selected.

Any help is appreciated.


1 solution

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    foreach (GridViewRow gvRow in GridView5.Rows)
        Control ctrl = gvRow.FindControl("DropDownList2");
        DropDownList ddl = ctrl as DropDownList;
        if (ddl != null)
            ddl.SelectedIndex = DropDownList2.SelectedIndex;

try this article for more reference
Accessing a DropDownList inside a GridView[^]
zyck 7-Feb-12 18:40pm
please vote when this code solution will help you

thanks (n_n)

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