1

我是 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)?如果我让你感到困惑,请告诉我。

任何有关更好方法的建议将不胜感激。提前致谢!

4

2 回答 2

1

您可以对两个 DDL 使用相同的函数。所以你会有:

<asp:DropDownList id="insertVendorName" .... OnSelectedIndexChanged="ddlVendor_SelectedIndexChanged" >

现在在函数本身内部,您需要获取对正确 DDL 的引用,即调用该函数的 DDL。这sender就是为了。

DropDownList ddlVendor = (DropDownList)sender;
于 2012-10-11T17:45:07.447 回答
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 的(否则很好的解决方案)略好,因为如果一个下拉列表的事件逻辑需要更改,而另一个则不需要更改,则必须重新构建代码。

如果单独的函数只执行共享逻辑,并将特定于上下文的逻辑放在相应的事件处理程序中,那也会更好。这可能需要您让单独的函数返回一些东西。

于 2012-10-11T17:51:32.050 回答