Click here to Skip to main content
14,880,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My ASPX Code:

<asp:Panel ID="Panel1" runat="server" CssClass="table-responsive">

               <asp:GridView ID="grdMain" runat="server" ShowFooter="True" AllowPaging="True" PageSize="25"
               AutoGenerateColumns="False" DataKeyNames="ItemCode"
               CssClass="table table-hover table-striped grdViewTable"  >


                   <asp:TemplateField HeaderText="Select to Return" ItemStyle-CssClass="hidden-xs" HeaderStyle-CssClass="hidden-xs">

                           <asp:CheckBox ID="chkRow" runat="server"
                               oncheckedchanged="chkRow_CheckedChanged" AutoPostBack="true" />
                           <asp:CheckBox ID="CheckBox1" runat="server" />

                       <FooterTemplate >
                           <asp:Button ID="btnIssue" runat="server" Text="Issue" Height="32px" ToolTip="Checkbox selection required to Issue Item."
                               Width="69px" onclick="btnIssue_Click" BackColor="#0066CC" Font-Bold="True"
                       <HeaderStyle CssClass="hidden-md" />
                       <ItemStyle CssClass="hidden-md" />

                   <asp:BoundField DataField="ItemCode" HeaderText="ItemCode" SortExpression="ItemCode"
                       ItemStyle-CssClass="hidden-xs" HeaderStyle-CssClass="hidden-xs" Visible="true"  >
                       <HeaderStyle CssClass="hidden-xs" />
                       <ItemStyle CssClass="hidden-xs" />

                   <asp:BoundField DataField="ItemName" HeaderText="Item Name"
                       SortExpression="ItemName"  ItemStyle-CssClass="hidden-md"
                       HeaderStyle-CssClass="hidden-md" >
                       <HeaderStyle CssClass="hidden-md" />
                       <ItemStyle CssClass="hidden-md" />

                   <asp:TemplateField Visible="True" HeaderText="Return Remarks">
                           <asp:TextBox ID="GridTextBoxRemarks" runat="server" Height="40px" TextMode="MultiLine"
                               Width="214px" ForeColor="#080808"></asp:TextBox>
                           <asp:CheckBox ID="CheckBox2" runat="server" />

                   <asp:TemplateField HeaderText="Status">
                           <asp:DropDownList ID="myddl" runat="server" AutoPostBack="True"

                            DataSource='<%# Bind_StatusData() %>'
                            DataTextField="StatusName" DataValueField="StatusID"

                           <asp:Label ID="gridLabel" runat="server" Text=""></asp:Label>

                           <asp:LinkButton ID="lbtnGridDelete" runat="server" Font-Bold="true"
                               Font-Size="Large" ForeColor="#f6740f" OnClick="lbtnGridDelete_Click">Delete</asp:LinkButton>


               <RowStyle CssClass="rowStyle" />
               <HeaderStyle CssClass="headerStyle" BackColor="Beige" Font-Names="Comic Sans MS" />
               <FooterStyle CssClass="footerStyle" />
               <PagerStyle  HorizontalAlign ="Center" />
               <PagerSettings Mode="NumericFirstLast" PageButtonCount="4" FirstPageText="First" LastPageText="Last" />

What I have tried:

Showing ERROR:
cannot convert type 'system.web.ui.control' to 'dropdownlist'

protected void grdMain_SelectedIndexChanged(object sender, EventArgs e)

      DropDownList ddl = (DropDownList)grdMain.SelectedRow.FindControl("ddlMappingStatus") as DropDownList;

Updated 3-Jan-21 21:03pm

Why are you trying to cast and convert?
type varname = (type) othervar;
That is a cast.
type varname = othervar as type;
That is using the as operator to check the type, and return null if a cast is not possible.
Sint the cast has a higher precedence, you are doing a cast operation (which will throw and exception if it doesn't work) then doing a check-and-cast-if-possible as operator (which will return the instance as the type or null) on the result ...

Remove the cast, and check the result for null.

But ... since you are getting an exception, you will get a null from the as operator - so what you need to do is use the debugger to find out exactly what FindControl is returning - since it clearly isn't a DropDownList, and the finding out what it is should help you to work out why not.
3 things:
1. You use a cast as well as as operator. You shouldn't cast. Just use as operator and it will handle null value in case findcontrol returns nothing. Your case, it is returning something though.
2. Don't think you have a dropdown named ddlMappingStatus. Instead, seems you want to use myddl
3. The way you are accessing the dropdown is odd. You should make use of RowCommand and then access the dropdown. Complete example here: Find (Access) control inside GridView in RowDataBound and RowCommand events of ASP.Net GridView[^]

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    //Determine the RowIndex of the Row whose Button was clicked.
    int rowIndex = Convert.ToInt32(e.CommandArgument);
    //Reference the GridView Row.
    GridViewRow row = GridView1.Rows[rowIndex];
    //Find the TextBox control.
    TextBox txtName = (row.FindControl("txtName") as TextBox);
    //Find the DropDownList control.
    DropDownList ddlCountries = (row.FindControl("ddlCountries") as DropDownList);
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + txtName.Text + "\\nCountry: " + ddlCountries.SelectedItem.Value + "');", true);
Thanks but Showing Same ERROR:

cannot convert type 'system.web.ui.control' to 'dropdownlist'
Finally i got the solution.

My class file name was 'dropdownlist' that's why its conflict with class name.

DropDownList objDropDownList = new DropDownList();

Change to
XDropDownList objDropDownList = new XDropDownList();

Finally No Error:
DropDownList drList = gvr.FindControl("myddl") as DropDownList;

Thanks to All
for trying to solve this problem.

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