Asynchronous
Asynchronous

Reputation: 3977

How to call specific method based on user selection in DropDownList in GridView

I have a GridView illustrated in the screen shots below: I need to call a specific Method based on the item selected by the user. I tried several approach, included using SelectedValue and OnDataBind events not getting it right. I am having trouble getting access to the DropDownList and having it's event call the Methods.

Default view:

enter image description here

Selection changed:

enter image description here

Code:

<asp:GridView ID="grdLoadData" AutoGenerateColumns="false" runat="server">

<Columns>
<asp:TemplateField HeaderText="Example">
<ItemTemplate>
<asp:DropDownList ID="ddlExampleDropDownList"  runat="server" 
     AutoPostBack="true" Width="100">
    <asp:ListItem Text="---- Select --" Value="select" />
    <asp:ListItem Text="Do Task A" Value="Task A" />
    <asp:ListItem Text="Do Task B" Value="Task B" />
</asp:DropDownList> 
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="LAST_NAME" HeaderText="Last Name" />
<asp:BoundField DataField="FIRST_NAME" HeaderText="First Name" />
<asp:BoundField DataField="MiDDLE_NAME" HeaderText="Middle Name" />

<asp:TemplateField HeaderText="Customer ID">
<ItemTemplate>
<asp:Label ID="lblCustomerID" Text='<%#Eval("CUST_ID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>

Code Behind:

public partial class example : System.Web.UI.Page
{

    private DbConnection GetDatabaseConnection(string name)
    {
        ConnectionStringSettings settings =
        ConfigurationManager.ConnectionStrings[name];
        DbProviderFactory factory = DbProviderFactories.GetFactory
        (settings.ProviderName);
        DbConnection conn = factory.CreateConnection();
        conn.ConnectionString = settings.ConnectionString;
        return conn;
    }

    public void LoadData()
    {
        using (SqlConnection connection =  
            (SqlConnection)(GetDatabaseConnection("EDMS")))
        {
            try
            {
                connection.Open();
                SqlCommand cmd = new 
                    SqlCommand("ExampleStoredProcedure", connection);

                grdLoadData.DataSource = cmd.ExecuteReader();
                grdLoadData.DataBind();
            }
            catch(SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
    } 

    //Call if task A selected in DDL
    public void DoTaskA()
    {
        // do things here
    }

   //Call if task B selected in DDL
    public void DoTaskB()
    {
        // do things here
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }  
    }
}

Upvotes: 0

Views: 1157

Answers (1)

Just code
Just code

Reputation: 13801

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            GridViewRow row = (GridViewRow)ddl.Parent.Parent;
            int idx = row.RowIndex;
            if (ddl.SelectedValue == "Task A")
            {
                DoTaskA();
            }
            else if (ddl.SelectedValue == "Task B")
            {

                DoTaskB();
            }


        }
        public void DoTaskA()
        {
            // do things here
        }

        //Call if task B selected in DDL
        public void DoTaskB()
        {
            // do things here
        }
  • And yea, Don't forget to include that in your design

           <asp:DropDownList ID="ddlExampleDropDownList"  runat="server" 
                AutoPostBack="true" Width="100" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
               <asp:ListItem Text="---- Select --" Value="select" />
               <asp:ListItem Text="Do Task A" Value="Task A" />
               <asp:ListItem Text="Do Task B" Value="Task B" />
           </asp:DropDownList>
    

Upvotes: 1

Related Questions