我有以下内容,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();
}
}
我建议您将“ 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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句