[英]How can we handle the onselectedindexchanged event of a dropdownlist in a gridview/datalist?
I have a simple datalist with a dropdownlist and a textbox.我有一个带有下拉列表和文本框的简单数据列表。
When the dropdownlist selected index changes, I want to load a value into the textbox in that listbox item (ie, the textbox on that particular row).当下拉列表选定的索引更改时,我想将一个值加载到该列表框项中的文本框(即该特定行上的文本框)。
<ItemTemplate>
<asp:DropDownList runat="server"
ID="ddlCategory" AutoPostBack="true"
DataTextField="category"
DataValueField="category_code"
OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" />
<br />
Code<asp:TextBox
runat="server"
ID="txtOutputCode"
Text='<%# Bind("output_code") %>' />
</ItemTemplate>
How do I do this?我该怎么做呢?
The challenge I'm facing is how to find the corresponding textbox to update.我面临的挑战是如何找到相应的文本框进行更新。
Eg for a button I'd pass a commandname, and command arguments.例如,对于一个按钮,我会传递一个命令名和命令参数。 And then I'd handle the event in the gridview or datalist to find the corresponding textbox and update the text.
然后我会处理 gridview 或 datalist 中的事件以找到相应的文本框并更新文本。 What can we do in case of a selectedindex change of a dropdownlist?
如果下拉列表的选定索引发生更改,我们该怎么办?
I guess this should work.我想这应该有效。 Try this...
试试这个...
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
var ddlList= (DropDownList)sender;
var row = (GridViewRow)ddlList.NamingContainer;
//get the Id of the row
var Id = Convert.ToInt32(((Label)row.FindControl("IdColumn")).Text);
}
What you should do is the following:你应该做的是:
private SomeObject o = new SomeObject();
private void o_SomeEvent(...) {
}
public TheConstructor() {
this.o.SomeEvent += new SomeHandler(o_SomeEvent);
}
which means you have to create a new dropdownlist and attach its event to the dropdownlist you have in the gridview on itemdatabound这意味着您必须创建一个新的下拉列表并将其事件附加到您在 itemdatabound 的 gridview 中的下拉列表
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlcouse = (DropDownList)sender;
DataListItem Grow = (DataListItem)ddlcouse.NamingContainer;
foreach (DataListItem dst in dstAllSite.Items)
{
DropDownList ddl_AccountInfo = (DropDownList)dst.FindControl("ddlAccountInfo");
if (ddlcouse.SelectedIndex > 0)
{
BindAccountDDL(ddl_AccountInfo, ddlcouse.SelectedValue.Trim());
ddlcouse.Focus();
}
}
}
private void BindAccountDDL(DropDownList ddlAccountInfo, string ddlcouse)
{
csPL.ddlcouse = ddlcouse;
DataTable dt = csBL.BindAccountInfoDDL(csPL);
if (dt.Rows.Count > 0)
{
ddlAccountInfo.DataSource = dt;
ddlAccountInfo.DataTextField = "BankName";
ddlAccountInfo.DataValueField = "BankId";
ddlAccountInfo.DataBind();
ddlAccountInfo.Items.Insert(0, "Select");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.