如何根据GridView中DropDownList中的用户选择调用特定方法

时间:2015-04-03 04:50:51

标签: c# asp.net

我在下面的屏幕截图中说明了GridView:我需要根据用户选择的项目调用特定的Method。我尝试了几种方法,包括使用SelectedValueOnDataBind事件来解决问题。我无法访问DropDownList并将其事件调用Methods

默认视图:

enter image description here

选择已更改:

enter image description here

代码:

<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>

代码背后:

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();
        }  
    }
}

1 个答案:

答案 0 :(得分:1)

 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
        }
  • 是的,别忘了把它包含在你的设计中

           <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>