locked
How to find the row that selected when a control in that row was activated/chosen ? RRS feed

  • Question

  • User-1682007629 posted

    I have a GridView control, and in the first column of the GridView I have a drop down listbox.

    If someone selects and item from a drop down list box in a row, I have my code post back to to the event handler for the drop down listbox.
        protected void ddlParameterName_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

    What I need to do is to be able to determine which row was selected and then to retrieve the selected value from the drop
    down listbox which was selected. I am assuming I can use the find control method to get the item selected in the drop down list box.
    Something like this: row.FindControl("ddlParameterName")).SelectedItem.ToString()

    The problem though, is how do I determine which row of the Grid was selected when the app user changed the drop down listbox
    item in that particular row? So how to get a handle on that row is my question. I did not see a row property or method
    as part of the "GridViewCommandEventArgs e" so  I need help.

    I don't know if it matters, but here is a snippet of my drop down listbox from my grid view control

     

     


                    <asp:TemplateField HeaderText="Search Item">

                        <ItemTemplate >
                            <asp:DropDownList ID="ddlParameterName" runat="server" AppendDataBoundItems="True"
     
                                DataSourceID="SqlDataSource1" DataTextField="ParameterName"
                                DataValueField="ColumnName" Width="139px" AutoPostBack="True"
                                onselectedindexchanged="ddlParameterName_SelectedIndexChanged">
                                <asp:ListItem>--Select--</asp:ListItem>

                            </asp:DropDownList>

                            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                                ConnectionString="<%$ ConnectionStrings:theConnectionString %>"
                               
                                SelectCommand="SELECT [ParameterName], [ColumnName] FROM [tblSearchParametersLitrine]">
                            </asp:SqlDataSource>
                        </ItemTemplate>


      // Also, I have a GridView1_RowCommand event handler, but I don't know if that could be of any use in order to solve
      //   this problem?

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

     

            if (e.CommandName == "Delete1") // This code to get the row to delete
            {

            }

        }

    Thursday, August 27, 2009 5:06 PM

Answers

  • User1383809551 posted

    Hello,

    You can use this to find the selected value of the dropdown list.

    sender is nothing but the control which raises the event.

    protected void ddlParameterName_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            string Value = ddl.SelectedValue;
        }


    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2009 5:11 PM

All replies

  • User1383809551 posted

    Hello,

    You can use this to find the selected value of the dropdown list.

    sender is nothing but the control which raises the event.

    protected void ddlParameterName_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            string Value = ddl.SelectedValue;
        }


    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2009 5:11 PM
  • User2130758966 posted

    I dont think I get this still - I even read it twice!

    Are you saying that:

    • Every row has a DropDownList
    • DropDownList has autopostback set to true
    • You want to handle the DropDownList selected index change
    • And figure out which row the selected index change occured on

    Hmm hang on a minute I just re-read why you want to get the row.

    You can get the dropdownlist like so:

    protected void ddlParameterName_SelectedIndexChanged(object sender, EventArgs e)
    {
      DropDownList ParameterName = (DropDownList)sender;
    
      string value = ParameterName.SelectedValue;
       
    }


    You can always cast the sender object to the type of the control that it came from to get a reference to itself.

    Its a good way to do things because it makes your code more reusable.

    Its not tied to the name ddlParameterName so you could use it in another SelectedIndexChanged event.

    Thursday, August 27, 2009 5:25 PM