如何从GridView获取值并将值传递给GridView中由DropDownList调用的Method?

时间:2015-04-03 05:44:13

标签: c# asp.net

我有以下GridView包含DropDownList,该SelectedValue根据Customer ID调用方法:

在进行选择的行上,如何从GridView获取<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();
         }
    }

代码背后:

{{1}}

2 个答案:

答案 0 :(得分:2)

我建议您将“CustID”作为属性添加到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活动中获取您的CustomerID应该是这样的:

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

答案 1 :(得分:0)

另一种不改变标记的方法是,

Label myLabel = gridviewRow.FindControl("lblCustomerID") as Label;
string customerID = myLabel.Text;