2013-10-03 29 views
0

當我的新行添加到上面選擇下拉列表列表視圖中被清除。什麼是這個問題的PLZ的原因建議我添加新行的ListView刪除上面選擇下拉菜單中清除

protected void Page_Load(object sender, EventArgs e) 
{ 
      if (!Page.IsPostBack) 
      { 
       FirstListViewRow(); 
       BindDataToGridviewDropdownlist(); 
      } 
     } 
     protected void BindDataToGridviewDropdownlist() 
     { 
      DataSet dsDept = new DataSet(); 
      dsDept.ReadXml(Server.MapPath("XMLFile2.xml")); 
      DataView dv = dsDept.Tables[0].DefaultView; 
      foreach (var list in listview1.Items) 
      { 
       if (list.ItemType == ListViewItemType.DataItem) 
       { 
        DropDownList ddf = (DropDownList)list.FindControl("ddldatatype"); 
        ddf.DataSource = dv; 
        ddf.DataTextField = "value"; 
        ddf.DataBind(); 
        ddf.Items.Insert(0, new ListItem("--Select--", "0")); 
       } 
      } 
     } 
     private void FirstListViewRow() 
     { 
      DataTable dt = new DataTable(); 
      DataRow dr = null; 
      dt.Columns.Add(new DataColumn("OrderNo", typeof(string))); 
      dt.Columns.Add(new DataColumn("ColumnTitle", typeof(string))); 
      dt.Columns.Add(new DataColumn("Datatype", typeof(string))); 
      dt.Columns.Add(new DataColumn("Examples", typeof(string))); 
      dt.Columns.Add(new DataColumn("Options", typeof(string))); 
      dt.Columns.Add(new DataColumn("Delete", typeof(string))); 
      dr = dt.NewRow(); 
      dt.Rows.Add(dr); 
      dr["OrderNo"] = 1; 
      Session["CurrentTable"] = dt; 
      listview1.DataSource = dt; 
      listview1.DataBind(); 
     } 

     private void AddNewRow() 
     { 
      int rowIndex = 0; 
      if (Session["CurrentTable"] != null) 
      { 
       DataTable dtCurrentTable = (DataTable)Session["CurrentTable"]; 
       DataRow drCurrentRow = null; 
       if (dtCurrentTable.Rows.Count > 0) 
       { 
        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
        { 
         Label TextBoxorder = (Label)listview1.Items[rowIndex].FindControl("txtorder"); 
         TextBox TextBoxcolumnname = (TextBox)listview1.Items[rowIndex].FindControl("txtcolumnname"); 
         DropDownList DropDatatype = (DropDownList)listview1.Items[rowIndex].FindControl("ddldatatype"); 
         DropDownList Dropexample = (DropDownList)listview1.Items[rowIndex].FindControl("ddlexamples"); 
         TextBox TextBoxoptions = (TextBox)listview1.Items[rowIndex].FindControl("txtoptions"); 
         CheckBox Checkdel = (CheckBox)listview1.Items[rowIndex].FindControl("chkdel"); 

         drCurrentRow = dtCurrentTable.NewRow(); 
         drCurrentRow["OrderNo"] = i + 1; 

         // dtCurrentTable.Rows[i - 1]["Order"] = TextBoxorder.Text; 
         dtCurrentTable.Rows[i - 1]["ColumnTitle"] = TextBoxcolumnname.Text; 
         dtCurrentTable.Rows[i - 1]["Datatype"] = DropDatatype.Text; 
         dtCurrentTable.Rows[i - 1]["Examples"] = Dropexample.Text; 
         dtCurrentTable.Rows[i - 1]["Options"] = TextBoxoptions.Text; 
         dtCurrentTable.Rows[i - 1]["Delete"] = Checkdel.Text; 

         rowIndex++; 
        } 
        dtCurrentTable.Rows.Add(drCurrentRow); 
        Session["CurrentTable"] = dtCurrentTable; 
        listview1.DataSource = dtCurrentTable; 
        listview1.DataBind(); 
        Label txn = (Label)listview1.Items[rowIndex].FindControl("txtorder"); 
        txn.Focus(); 
       } 
      } 
      else 
      { 
       Response.Write("Session is null"); 
      } 
      BindDataToGridviewDropdownlist(); 
     } 
     protected void btnGenerate_Click(object sender, EventArgs e) 
     { 


     } 
     protected void btnAdd_Click(object sender, EventArgs e) 
     { 
      AddNewRow(); 
     } 

     protected void btndelete_Click(object sender, EventArgs e) 
     { 
      DataTable dt = new DataTable(); 
      if (Session["CurrentTable"] != null) 
      { 
       dt = (DataTable)Session["CurrentTable"]; 
       int j = 0; 
       for (int i = 0; i < listview1.Items.Count; i++) 
       { 
        ListViewDataItem items = listview1.Items[i]; 
        CheckBox chkBox = (CheckBox)items.FindControl("chkdel"); 

        if (chkBox.Checked == true) 
        { 
         dt.Rows.RemoveAt(j); 
         dt.AcceptChanges(); 
        } 
        else 
        { 
         j++; 
        } 
       } 
       Session["CurrentTable"] = dt; 
       listview1.DataSource = dt; 
       listview1.DataBind(); 
       BindDataToGridviewDropdownlist(); 
      } 
     } 

     protected void btnClear_Click(object sender, EventArgs e) 
     { 
      listview1.Items.Clear(); 
     } 
    } 
} 

回答

0

添加屬性的DataKeyNames在此的ListView這樣的:

<asp:listview id="listview1" runat="server" datakeynames="Datatype" xmlns:asp="#unknown"></asp:listview> 

並添加BindDataToGridviewDropdownlist方法內這些行:

ListViewDataItem di = (ListViewDataItem)list; 
       string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString(); 
       ddf.SelectedValue = dataType; 

這裏是你如何BindDataToGridviewDropdownlist方法看起來像:

protected void BindDataToGridviewDropdownlist() 
    { 
     DataSet dsDept = new DataSet(); 
     dsDept.ReadXml(Server.MapPath("XMLFile2.xml")); 
     DataView dv = dsDept.Tables[0].DefaultView; 
     foreach (var list in listview1.Items) 
     { 
      if (list.ItemType == ListViewItemType.DataItem) 
      { 
       DropDownList ddf = (DropDownList)list.FindControl("ddldatatype"); 
       ddf.DataSource = dv; 
       ddf.DataTextField = "value"; 
       ddf.DataBind(); 
       ddf.Items.Insert(0, new ListItem("--Select--", "0")); 

       ListViewDataItem di = (ListViewDataItem)list; 
       string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString(); 
       ddf.SelectedValue = dataType; 
      } 

     } 
    } 
相關問題