我是ASP.NET新手。我在我的DetailsView中有这个:
<asp:TemplateField HeaderText="VendorName">
<InsertItemTemplate>
<asp:DropDownList id="insertVendorName" datasourceid="VendorSqlDataSource"
datatextfield="VendorName" DataValueField="VendorID"
SelectedValue='<%# Bind("VendorID") %>'
runat="server" AutoPostBack="true">
<asp:ListItem Text="Select" Value="-1" />
</asp:DropDownList>
</InsertItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="updateVendorName"
datasourceid="VendorSqlDataSource" AutoPostBack="true"
datatextfield="VendorName" DataValueField="VendorID"
SelectedValue='<%# Bind("VendorID") %>'
runat="server" OnSelectedIndexChanged="ddlVendor_SelectedIndexChanged" />
</EditItemTemplate>
</asp:TemplateField>
这是我编辑模式的代码,它将刷新下拉列表。
protected void ddlVendor_SelectedIndexChanged (object sender, EventArgs e)
{
DropDownList ddlVendor = (DropDownList)DetailsView1.FindControl("updateVendorName");
DropDownList ddlVendorBU = (DropDownList)DetailsView1.FindControl("VendorBUName");
/*
if (DetailsView1.CurrentMode != DetailsViewMode.Edit)
{
DropDownList ddlVendor = (DropDownList)DetailsView1.FindControl("insertVendorName");
int VendorID = Convert.ToInt32(ddlVendor.SelectedValue.ToString());
}
else
{
DropDownList ddlVendor = (DropDownList)DetailsView1.FindControl("VendorName");
int VendorID = Convert.ToInt32(ddlVendor.SelectedValue.ToString());
}
*/
if (ddlVendorBU != null)
{
Response.Write("SelectChanged");
int VendorID = Convert.ToInt32(ddlVendor.SelectedValue.ToString());
Response.Write("VendorID: " + VendorID);
ddlVendorBU.Items.Clear();
string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT VendorBUID, VendorBUName FROM dbo.MDF_VendorBU WHERE VendorID = @VendorID";
cmd.Parameters.AddWithValue("@VendorID", VendorID);
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
/*
if (objDs.Tables[0].Rows.Count > 0)
{
ddlVendorBU.DataSource = objDs.Tables[0];
ddlVendorBU.ItemTextField = "VendorBUName";
ddlVendorBU.ItemValueField = "VendorBUID";
ddlVendorBU.DataBind();
}
*/
if (objDs.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in objDs.Tables[0].Rows)
{
ddlVendorBU.Items.Insert(0, new ListItem((dr["VendorBUName"]).ToString(), (dr["VendorBUID"]).ToString() ));
}
}
}
}
正如你在EditItemTemplate中看到的,我有dropdownlist的updateVendorName ID,这个ddl调用ddlVendor_SelectedIndexChange来刷新另一个列表。现在,我想使用相同的代码刷新列表,但在InsertItemTemplate中。由于dropdownlist ID与“insertVendorName”不同,我如何使用相同的ddlVendor_SelectedIndexChange代码,但在插入模式下使用不同的ddl ID(insertVendorName)?如果我让你困惑,请告诉我。
任何有关更好方法的建议都将受到赞赏。 提前谢谢!
答案 0 :(得分:1)
您可以对两个DDL使用相同的功能。所以你有:
<asp:DropDownList id="insertVendorName" .... OnSelectedIndexChanged="ddlVendor_SelectedIndexChanged" >
现在在函数本身内部,您需要获得对正确DDL的引用,即调用该函数的DDL。这就是sender
的用途。
DropDownList ddlVendor = (DropDownList)sender;
答案 1 :(得分:1)
简单来说,您应该将事件处理程序中的代码放在单独的函数中并从那里调用它。我会给你一个骨架结构:
protected void ddlVendor_SelectedIndexChanged (object sender, EventArgs e)
{
MyFunction(sender);
}
protected void ddlInsertVendorName_SelectedIndexChanged (object sender, EventArgs e)
{
MyFunction(sender);
}
protected void MyFunction(Object sender)
{
DropDownList ddl = (DropDownList)sender //ddl will contain the ID etc...
//shared logic here
if(ddl.ID.equals("ddlVendor"))
{
//edit mode
}
else
{
//insert mode
}
}
这个解决方案略好于System Down(否则是很好的解决方案),因为如果一个下拉列表的事件逻辑需要更改,而另一个下拉列表的事件逻辑不需要更改,则必须重新构建代码。
如果单独的函数仅执行共享逻辑,并将特定于上下文的逻辑放在相应的事件处理程序中,那也会更好。这可能需要你让单独的函数返回一些东西。