这是一个小型注册表单,我想做的就是当用户点击按钮时我想通过使用数据表和会话在表格中输入详细信息
<form id="form1" runat="server">
<div>
<center>
<table>
<tr>
<td>
First Name
</td>
<td>
<asp:TextBox ID="TxtFirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name
</td>
<td>
<asp:TextBox ID="TxtLastName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Location
</td>
<td>
<asp:TextBox ID="TxtLocation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Mobile No
</td>
<td>
<asp:TextBox ID="TxtMobileNo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="BtnSave" Text="SAVE" runat="server" onclick="BtnSave_Click"/>
</td>
<td>
<asp:Button ID="BtnCancel" Text="CANCEL" runat="server" />
</td>
</tr>
</table>
</center>
<center>
<asp:GridView ID="GridDataTable" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name"></asp:TemplateField>
<asp:TemplateField HeaderText="Location"></asp:TemplateField>
<asp:TemplateField HeaderText="Mobile No"></asp:TemplateField>
</Columns>
</asp:GridView>
</center>
</div>
</form>
这是我的代码背后但它不起作用帮助我这个
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void Bind()
{
System.Data.DataTable workTable = new System.Data.DataTable("RegTable");
workTable.Columns.Add(new DataColumn("FirstName", typeof(String)));
workTable.Columns.Add(new DataColumn("LastName", typeof(String)));
workTable.Columns.Add(new DataColumn("Location", typeof(String)));
workTable.Columns.Add(new DataColumn("MobileNo", typeof(Int32)));
Session["RegDetails"] = workTable;
GridDataTable.DataSource = workTable;
GridDataTable.DataBind();
}
protected void BtnSave_Click(object sender, EventArgs e)
{
System.Data.DataTable dt = (System.Data.DataTable)Session["RegDetails"];
DataRow dr = dt.NewRow();
dr["FirstName"] = TxtFirstName.Text.ToString();
dr["LastName"] = TxtLastName.Text.ToString();
dr["Location"] = TxtLocation.Text.ToString();
dr["MobileNo"] = TxtMobileNo.Text;
dt.Rows.Add(dr);
Session["RegDetails"] = dt;
GridDataTable.DataSource = dt;
GridDataTable.DataBind();
}
答案 0 :(得分:1)
唯一的问题是您的GridView
标记。你的代码背后的代码是复制&amp;验证了相同。
如您所见,您的<asp:label>
中的<asp:TemplateField>
等任何字段都没有数据。
此外,您在某些<ItemTemplates>
中遗漏了<asp:TemplateFields>
。
因此,将GridView标记设置为:
<asp:GridView ID="GridDataTable" runat="server"
AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
<asp:Label Text='<%#Eval("FirstName")%>' runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label Text='<%#Eval("LastName")%>' runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label Text='<%#Eval("Location")%>' runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile No">
<ItemTemplate>
<asp:Label Text='<%#Eval("MobileNo")%>' runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 1 :(得分:-1)
首先将数据插入数据库,然后通过gridview ....
检索