DetailsView中的Eval/Binding下拉列表



我在detailsview中有一个dropdownlist,它从一个表中获取值,并将所选值绑定到另一个表。我遇到的问题是,每当回发发生时,我从中获取ddl值的表都会改回默认读取。这使得所选的值总是变为空(这是列表的第一个值)

我试过推杆!IsPostBack在页面中_load:

   if (!IsPostBack)
   {
        DetailsView1.DataBind();
   }

我确实有第二个ddl,它依赖于第一个列表,但它运行良好,它是第一个总是为空的列表,不会保存所选的值。

这是第一个ddl:

  <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" 
    AutoPostBack="True" DataSourceID="SQLLEAVECODE" DataTextField="LEAVETYPE" 
    DataValueField="LEAVECODE" Height="18px" style="text-transform:uppercase;"
    onselectedindexchanged="DropDownList1_SelectedIndexChanged"
    SelectedValue='<%# bind("reqleavecode") %>' Width="145px">
    <asp:ListItem></asp:ListItem>
  </asp:DropDownList>

我似乎想不通,我想这可能与我的束缚有关。

    public string lvtype;
    public string lvrequest;
    private DataSet GetData()
    {
        ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
        var sql = "SELECT LEAVETYPE, LEAVECODE FROM TESTBWTIME.BWLVTYPE ORDER BY LEAVECODE";
        using (iDB2Connection conn = new iDB2Connection(GetConnectionString()))
        {
            conn.Open();
            using (iDB2Command cmd = new iDB2Command(sql, conn))
            {
                cmd.DeriveParameters();
                using (iDB2DataAdapter da = new iDB2DataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    return ds;
                }
            }
        }
    }
    private String GetConnectionString()
    {
        ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
        return cssc["connStringNET"].ToString();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList ddl1 = (DropDownList)(DetailsView1.FindControl("DropDownList6"));
        DropDownList ddl2 = (DropDownList)(DetailsView1.FindControl("DropDownList5"));
        DataSet ds = GetData();
        if (!Page.IsPostBack)
        {
            ddl1.DataSource = ds;
            ddl1.DataBind();
            lvtype = ddl1.SelectedValue;
            ddl2.DataSource = ds;
            ddl2.DataBind();
            lvrequest = ddl2.SelectedValue;
        }
        else
        {
            lvtype = ddl1.SelectedValue;
            lvrequest = ddl2.SelectedValue;
        }
       }

我遇到了同样的问题,我使用以下技术解决了它。请根据您的需要修改此代码。基本上,在该类的i-e全局顶部需要两个变量。然后您可以在Postback中获得dropdownlist的选定值和选定的索引更改事件,如下所示;

public string vendorId;
public string categoryId;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        dropDownListVendor.DataSource = CatalogAccess.GetVendors();
        dropDownListVendor.DataBind();
        vendorId = dropDownListVendor.SelectedValue;
        dropDownListCategory.DataSource = CatalogAccess.GetCategoriesInVendor(vendorId);
        dropDownListCategory.DataBind();
        categoryId = dropDownListCategory.SelectedValue;
    }
    else
    {
        vendorId = dropDownListVendor.SelectedValue;
        categoryId = dropDownListCategory.SelectedValue;
    }
}
protected void dropDownListVendor_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        dropDownListCategory.DataSource = CatalogAccess.GetCategoriesInVendor(vendorId);
        dropDownListCategory.DataBind();
    }
}

最新更新