GridView의 빈 템플릿을 통해 데이터를 삽입하면 .. 값이 DB에 삽입되지만 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)
자동 바인딩이 작동하려면 BoundField 인 열이 하나 이상 있어야합니다.
예 : 작동하지 않습니다.
<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>
이것은 작동합니다
<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>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다