2015-04-03 55 views
2

我有一個包含以下GridView一個DropDownList它調用基於SelectedValue的方法:如何獲取GridView中的值並將其傳遞給GridView中由DropDownList調用的方法?

在列,其中選擇正在取得,我如何才能從GridViewCustomer ID它作爲參數傳遞給方法被稱爲下面?

代碼:

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

<Columns> 
<asp:TemplateField HeaderText="Example"> 
<ItemTemplate> 
<asp:DropDownList ID="ddlExampleDropDownList" runat="server" 
    AutoPostBack="true" Width="100" 
    OnSelectedIndexChanged="ddlExampleDropDownList_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> 
</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 string DoTaskA(string customerId) 
    { 
     return customerId; 
    } 

    public string DoTaskB(string customerId) 
    { 
     return customerId; 
    } 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      LoadData(); 
     } 
    } 

    protected void ddlExampleDropDownList_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList dropDownList = (DropDownList)sender; 
     GridViewRow gridViewRow = (GridViewRow)dropDownList.Parent.Parent; 

     if(dropDownList.SelectedValue=="Task A") 
     { 
      //Pass Customer ID here 
      DoTaskA(); 
     } 
     else if(dropDownList.SelectedValue=="Task B") 
     { 
      //Pass Customer ID here 
      DoTaskB(); 
     } 
    } 
+0

所以,你想客戶ID傳遞給SelectedIndex_changed方法? – 2015-04-03 05:50:34

+0

是的:當選擇一個值時,無論什麼值,我需要獲取該行的客戶ID並將其傳遞給相應的DoTask'方法'。 – Asynchronous 2015-04-03 05:54:28

回答

2

,我建議你到 「客戶ID」 添加爲一個屬性,你的DropDownList控件象下面這樣:

<asp:DropDownList ID="ddlExampleDropDownList" runat="server" 
AutoPostBack="true" Width="100" CustID='<%#Eval("CUST_ID") %>' 
OnSelectedIndexChanged="ddlExampleDropDownList_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" /> 

並獲得你的SelectedIndexChanged事件您的客戶ID應該是這樣的:

protected void ddlExampleDropDownList_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DropDownList dropDownList = (DropDownList)sender; 
    GridViewRow gridViewRow = (GridViewRow)dropDownList.Parent.Parent; 

    string lsCustomerID = Convert.ToString(dropDownList.Attributes["CustID"]); 

    if(dropDownList.SelectedValue=="Task A") 
    { 
     //Pass Customer ID here 
     DoTaskA(lsCustomerID); 
    } 
    else if(dropDownList.SelectedValue=="Task B") 
    { 
     //Pass Customer ID here 
     DoTaskB(lsCustomerID); 
    } 
} 
+0

哇!這太棒了。對於同一個問題,還有其他的解決方案可以跨越一個代碼頁:這是非常簡單和完美的。再次感謝! – Asynchronous 2015-04-03 06:10:44

+0

樂意幫忙.. !! – Keval 2015-04-03 06:12:36

0

不改變您的標記將是另一種方法,

Label myLabel = gridviewRow.FindControl("lblCustomerID") as Label; 
string customerID = myLabel.Text; 
相關問題