2015-04-03 20 views
0

我在下面的屏幕截圖中顯示GridView:我需要根據用戶選擇的項目調用特定的Method。我嘗試了幾種方法,包括使用SelectedValueOnDataBind事件不正確。我無法訪問DropDownList,遇到事件請撥打Methods如何根據GridView中的DropDownList中的用戶選擇調用特定方法

默認視圖:

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

您可以通過添加提高RowCommand按鈕旁邊的DropDownList,這篇文章將告訴你如何d那,http://www.codeproject.com/Articles/21163/Trick-Tip-Raise-a-GridView-RowCommand-event-from-a – Adil 2015-04-03 04:54:27

回答

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

謝謝我的朋友! – Asynchronous 2015-04-03 05:16:16

相關問題