我是 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 中看到的,我有下拉列表的 updateVendorName ID,并且此 ddl 调用 ddlVendor_SelectedIndexChange 来刷新另一个列表。现在,我想使用相同的代码来刷新列表,但在 InsertItemTemplate 中。由于下拉列表 ID 不同“insertVendorName”,如何在插入模式下使用相同的 ddlVendor_SelectedIndexChange 代码,但使用不同的 ddl ID (insertVendorName)?如果我让你感到困惑,请告诉我。
任何有关更好方法的建议将不胜感激。提前致谢!