2012-08-23 41 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

相關問題