2013-06-03 6 views
0

사용자가 텍스트 상자에 무언가를 입력하고 사용자 유형을 데이터베이스에 삽입해야하는 격자보기가 있습니다.이 두 가지 문제 또는 문제점이 있습니다. "+"버튼 (뉴스 행 추가), 이전 행 데이터의 텍스트 상자 중 하나가 제거됩니다. 선택한 라디오 버튼도 제거됩니다. Heres는 문제가 무엇인지 스크린 샷입니다 :그리드보기에서 이전 데이터 설정

enter image description here

의 "+"버튼을 클릭 한 후는,이 같은 것으로집니다 :

enter image description here

이 내 코드 뒤에 다음과 같습니다

private void AddNewRowToGrid() 
    { 
     int rowIndex = 0; 

     CheckBox chkbox1 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox1"); 
     CheckBox chkbox2 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox2"); 

     if (ViewState["CurrentTable"] != null) 
     { 
      DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; 
      DataRow drCurrentRow = null; 
      if (dtCurrentTable.Rows.Count > 0) 
      { 
       for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
       { 
        //extract the TextBox values 
        TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
        TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 
        TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3"); 

        drCurrentRow = dtCurrentTable.NewRow(); 


        dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;  
        dtCurrentTable.Rows[i - 1]["Hints"] = box3.Text; 


        rowIndex++; 
       } 
       dtCurrentTable.Rows.Add(drCurrentRow); 
       ViewState["CurrentTable"] = dtCurrentTable; 

       GridView1.DataSource = dtCurrentTable; 
       GridView1.DataBind(); 
      } 
     } 
     else 
     { 
      Response.Write("ViewState is null"); 
     } 

     // Set Previous Data on Postbacks 
     SetPreviousData(); 
    } 

    private void SetInitialRow() 
    { 
     DataTable dt = new DataTable(); 
     DataRow dr = null; 
     dt.Columns.Add(new DataColumn("Question", typeof(string))); 
     dt.Columns.Add(new DataColumn("Hints", typeof(string))); 

     dr = dt.NewRow(); 
     dr["Question"] = string.Empty; 
     dr["Hints"] = string.Empty; 


     dt.Rows.Add(dr); 


     //Store the DataTable in ViewState 
     ViewState["CurrentTable"] = dt; 

     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 

    private void SetPreviousData() 
    { 
     int rowIndex = 0; 
     if (ViewState["CurrentTable"] != null) 
     { 
      DataTable dt = (DataTable)ViewState["CurrentTable"]; 
      if (dt.Rows.Count > 0) 
      { 
       for (int i = 0; i < dt.Rows.Count; i++) 
       { 
        TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
        TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 
        TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3"); 



        //Setting previous text to the respective textboxes based on columns. 
        box1.Text = dt.Rows[i]["Question"].ToString(); 
        box2.Text = dt.Rows[i]["Hints"].ToString(); 
        box3.Text = dt.Rows[i]["Hints"].ToString(); 


        Session["Question1"] = box1.Text; 
        rowIndex++; 
       } 
      } 
     } 
    } 

각 열에 하나의 텍스트 상자가있을 때 쉽게 할 수 있습니다. 그러나 이제는 각각에 2 개의 텍스트 상자가 있습니다. 열 ... 때문에 열 이름을 기반으로 텍스트 상자에 텍스트를 추가 할 수 있습니다 ... 또한, 내 라디오 단추 도움말을 유지하는 방법 감사합니다, 감사합니다!

+0

문제는 두 개의 텍스트 상자 값을 하나의 datatable 열에 저장한다는 것입니다. 데이터 테이블에는 각 텍스트 상자 당 하나씩 두 개의 열이있는 것이 좋습니다. HintsOne, HintsTwo 또는 일부 구분 기호를 사용하여 두 텍스트 상자 값을 병합하여 데이터 테이블의 한 열 (힌트)에 넣습니다. 그럼 Gridview로 표시하기 전에 그들을 분리 ... 그게 좋지 않으므로 첫 번째 접근 방식을 선호 (두 개의 열을 사용) – Munawar

+0

그래, 나는 대안으로 생각하지만이 방법을 밖으로 일하고 싶습니다 .. 가능한 경우 duno .. Logged – user2376998

+0

데이터 열에 넣기 전에 두 텍스트 상자의 값을 병합하지 않는 한 하나의 열에는 항상 단일 텍스트 상자의 값이 있습니다. dt.Rows [i] [ "Hints"]. ToString(); (당신이 textbox3에서 저장 한 것이 무엇이든간에 대부분의 경우) 두 개의 데이터 열을 가지거나 데이터 테이블에 저장하기 전에 값을 병합하고 텍스트 상자에 다시 넣기 전에 값을 분할하는 옵션이 있습니다. – Munawar

답변

2
private void AddNewRowToGrid() 
     { 
      int rowIndex = 0; 

      CheckBox chkbox1 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox1"); 
      CheckBox chkbox2 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox2"); 

      if (ViewState["CurrentTable"] != null) 
      { 
       DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; 
       DataRow drCurrentRow = null; 
       if (dtCurrentTable.Rows.Count > 0) 
       { 
        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
        { 
         //extract the TextBox values 
         TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
         TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 
         TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3"); 

         drCurrentRow = dtCurrentTable.NewRow(); 


         dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;   
         dtCurrentTable.Rows[i - 1]["Hints"] = box2.Text; 
         dtCurrentTable.Rows[i - 1]["Hints1"] = box3.Text; 


         rowIndex++; 
        } 
        dtCurrentTable.Rows.Add(drCurrentRow); 
        ViewState["CurrentTable"] = dtCurrentTable; 

        GridView1.DataSource = dtCurrentTable; 
        GridView1.DataBind(); 
       } 
      } 
      else 
      { 
       Response.Write("ViewState is null"); 
      } 

      // Set Previous Data on Postbacks 
      SetPreviousData(); 
     } 

     private void SetInitialRow() 
     { 
      DataTable dt = new DataTable(); 
      DataRow dr = null; 
      dt.Columns.Add(new DataColumn("Question", typeof(string))); 
      dt.Columns.Add(new DataColumn("Hints", typeof(string))); 
      dt.Columns.Add(new DataColumn("Hints1", typeof(string))); 

      dr = dt.NewRow(); 
      dr["Question"] = string.Empty; 
      dr["Hints"] = string.Empty; 
      dr["Hints1"] = string.Empty; 


      dt.Rows.Add(dr); 


      //Store the DataTable in ViewState 
      ViewState["CurrentTable"] = dt; 

      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 

     private void SetPreviousData() 
     { 
      int rowIndex = 0; 
      if (ViewState["CurrentTable"] != null) 
      { 
       DataTable dt = (DataTable)ViewState["CurrentTable"]; 
       if (dt.Rows.Count > 0) 
       { 
        for (int i = 0; i < dt.Rows.Count; i++) 
        { 
         TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
         TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 
         TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3"); 



         //Setting previous text to the respective textboxes based on columns. 
         box1.Text = dt.Rows[i]["Question"].ToString(); 
         box2.Text = dt.Rows[i]["Hints"].ToString(); 
         box3.Text = dt.Rows[i]["Hints1"].ToString(); 


         Session["Question1"] = box1.Text; 
         rowIndex++; 
        } 
       } 
      } 
     } 

방금 ​​추가 한 나머지 코드 열은 이미 잘 작동했습니다.

+0

시간 내 주셔서 감사합니다. – user2376998

관련 문제