Click here to Skip to main content
15,893,381 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:

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

code behind...
C#
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.NET
 <asp:GridView ID="GridView5" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" DataSourceID="AccessDataSource6" 
            onRowDataBound="GridView5_RowDataBound" 
            >
                       <Columns>
               <asp:TemplateField  Visible="True" ShowHeader="
               False" HeaderText="Agreement Options" HeaderStyle-HorizontalAlign="Left" FooterText="Payable monthly in advance (including Agreement Options):" ItemStyle-BackColor="#CCCCCC">
                    <ItemTemplate>
                        <asp:Label ID="lblItemDescription" runat="server" Width="382"
Text='<%# Eval("itemDescription") %>'></asp:Label>
                    </ItemTemplate>

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

                        </asp:DropDownList>
                       
                    </ItemTemplate>
                </asp:TemplateField>
                 
                <asp:TemplateField>
                    <ItemTemplate>                  
                        <asp:Label ID="lblTotal" runat="Server"  Width="106"  />
                    </ItemTemplate>
                 <FooterTemplate>            
                 <asp:Label ID="lblTotalAmount" runat="server"  />        
                 </FooterTemplate>   
                </asp:TemplateField>
            
            </Columns>
            <FooterStyle Font-Bold="True" />
        </asp:GridView>


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

Any help is appreciated.

Thanks
Posted

1 solution

C#
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[^]
 
Share this answer
 
v2
Comments
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