2016-11-21 4 views
0

iはGridViewの空のテンプレートを介して任意のデータを挿入..値はDB に挿入されているが、挿入は、このように、すなわち表示されない空になった後にGridViewのは、これらの値を用いて(行うプログラムで)結合されていない空のテンプレートの後にデータテーブルと結合しません挿入された値。 GridViewのは、

は、ここで私は短いコードを維持するための唯一の値(DBMSテーブルのQUES内のフィールド)を挿入しています。

GridViewのHTMLマークアップコード

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" Height="146px" style="margin-top: 5px" Width="866px" > 
       <Columns> 
        <asp:TemplateField HeaderText="qno"> 
         <FooterTemplate> 
          <asp:TextBox ID="qno" runat="server"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Ques"> 
         <FooterTemplate> 
          <asp:TextBox ID="ques" runat="server" Height="16px" Width="78px"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="op1"> 
         <FooterTemplate> 
          <asp:TextBox ID="op1" runat="server" Height="16px" Width="76px"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="op2"> 
         <FooterTemplate> 
          <asp:TextBox ID="Top2" runat="server" Height="16px" Width="74px"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="op3"> 
         <FooterTemplate> 
          <asp:TextBox ID="op3" runat="server" Height="16px" Width="74px"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="op4"> 
         <FooterTemplate> 
          <asp:TextBox ID="op4" runat="server"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="ans"> 
         <FooterTemplate> 
          <asp:TextBox ID="ans" runat="server"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="marks"> 
         <FooterTemplate> 
          <asp:TextBox ID="marks" runat="server"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="UploadImage"> 
         <FooterTemplate> 
          <asp:TextBox ID="imageP" runat="server"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField> 
         <FooterTemplate> 
          <asp:Button ID="Button2" runat="server" Text="save" Width="78px" OnClick="Button2_Click1" /> 
         </FooterTemplate> 
        </asp:TemplateField> 
       </Columns> 


       <EmptyDataTemplate> 
    <tr style="background-color: Green;"> 
     <th scope="col"> 
      qno 
     </th> 
     <th scope="col"> 
      ques 
     </th> 
     <th scope="col"> 
      op1 
     </th> 
     <th scope="col"> 
      op2 
     </th> 
     <th scope="col"> 
      op3 
     </th> 
     <th scope="col"> 
      op4 
     </th> 
     <th scope="col"> 
      ans 
     </th> 
     <th scope="col"> 
      marks 
     </th> 
     <th scope="col"> 
      imageP 
     </th> 
     <th scope="col"> 

     </th> 
    </tr> 
    <tr> 
     <td> 
      <asp:TextBox ID="qno" runat="server" Height="16px" Width="68px" /> 
     </td> 
     <td> 
      <asp:TextBox ID="ques" runat="server" Height="16px" Width="68px" /> 
     </td> 
     <td> 
      <asp:TextBox ID="op1" runat="server" Height="16px" Width="68px" /> 
     </td> 
     <td> 
      <asp:TextBox ID="op2" runat="server" Height="16px" Width="68px" /> 
     </td> 
     <td> 
      <asp:TextBox ID="op3" runat="server" Height="16px" Width="68px" /> 
     </td> 
     <td> 
      <asp:TextBox ID="op4" runat="server" Height="16px" Width="68px" /> 
     </td> 
     <td> 
      <asp:TextBox ID="ans" runat="server" Height="16px" Width="68px"/> 
     </td> 
     <td> 
      <asp:TextBox ID="marks" runat="server" Height="16px" Width="68px"/> 
     </td> 
     <td> 
      <asp:TextBox ID="imageP" runat="server" Height="16px" Width="68px"/> 
     </td> 

     <td> 
      <asp:Button ID="Button2" runat="server" Text="Add" OnClick="Button2_Click1" CommandName = "EmptyDataTemplate" /> 
     </td> 
    </tr> 
</EmptyDataTemplate> 

      </asp:GridView> 

C#コード:

public partial class qpaper : System.Web.UI.Page 
{ 
String conStr = "Data Source=temporary; Initial Catalog=temporary1;Integrated Security=True"; 
String qpname = null; 


protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     this.BindData(); 
    } 
} 





private void BindData() 
{ 

    DataTable dt = new DataTable(); 
    using (SqlConnection con = new SqlConnection(conStr)) 
    { 
     string strQuery = "SELECT * FROM dbms"; 
     SqlCommand cmd = new SqlCommand(strQuery); 
     using (SqlDataAdapter sda = new SqlDataAdapter()) 
     { 
      cmd.Connection = con; 
      con.Open(); 
      sda.SelectCommand = cmd; 
      sda.Fill(dt); 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 
    } 
} 




protected void Button2_Click1(object sender, EventArgs e) // save button in gridview/empty template 
{ 

    Control control = null; 
    if (GridView1.FooterRow != null) 
    { 
     control = GridView1.FooterRow; 
    } 
    else 
    { 
     control = GridView1.Controls[0].Controls[0]; 
    } 
    string ques = (control.FindControl("ques") as TextBox).Text; 

    using (SqlConnection con = new SqlConnection(conStr)) 
    { 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.Connection = con; 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "insert into dbms (ques) VALUES (@ques)"; //inserting only one value here for shortening code length 
      cmd.Parameters.AddWithValue("@ques", ques); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
     } 
    } 
    Response.Redirect(Request.Url.AbsoluteUri); 

} 

}

表構造:

qno int , ques nvarchar(500), op1 nvarchar(50), op2 nvarchar(50), op3 nvarchar(50), op4 nvarchar(50), ans nvarchar(50), marks int, imagePath nvarchar(200) 

答えて

1

自動バインドを有効にするには、BoundFieldの列が少なくとも1つ必要です。例えば

:これはこれは

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"> 
    <Columns> 
     <asp:BoundField HeaderText="ID" DataField="id" /> 
     <asp:TemplateField HeaderText="Name"> 
      <ItemTemplate> 
       <asp:TextBox Id="txtName" runat="server"></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Address"> 
      <ItemTemplate> 
       <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 
+0

おかげで、それは今で固定だまいります

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"> <Columns> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:TextBox Id="txtName" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <ItemTemplate> <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

を動作しません! – ashish