2012-08-23 32 views
1

我在detailsview中有一个dropdownlist,它从一个表中获取其值并将所选值绑定到不同的表。我遇到的问题是,无论何时回发发生,我从中获取ddl值的表都会更改为默认读取。这使得该选择随时更改为空(这是列表中的第一个值)详细信息视图中的评估/绑定DropDownList

我试图把的IsPostBack在Page_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; 
     } 
     } 
+0

是在itemtemplate和edittemplate中呈现的下拉列表吗?并做这个帖子后面调用填充detailview的方法? – MrZulu

+0

下拉列表仅在中呈现,详细信息视图始终只处于插入模式。所以我不确定我碰巧在哪里填充它?我将输入的信息发送到由数据处理程序处理的表中。 – user1596472

+0

“reqleavecode”是否包含唯一数据? – MrZulu

回答

0

我讨厌同样的问题,我使用以下技术解决它。请根据您的需要修改此代码。基本上,您需要在该课程的全球顶级i-e上使用两个变量。然后,您可以在Postback中选择下拉列表的值并选择已编入索引的已更改事件,如下所示;

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(); 
    } 
} 
+0

好吧,我几乎已经修改正确,我唯一不确定的是这行:dropDownListVendor.DataSource = CatalogAccess.GetVendors();特别是CatalogAccess.GetVendors(),它并不适用于我的getData()方法,因为我不确定与CatalogAccess的等价物是什么。 – user1596472

+0

这是从DataAccessLayer返回的基本数据表。你可以在这里直接绑定你的数据表 –

+0

我编辑了我的代码,以显示我在做什么。如果你能给我更多的指导 – user1596472

相关问题