当我通过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)
答案 0 :(得分:1)
您需要至少有一个 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>