0

这是一个小的注册表单,我想做的就是当用户单击按钮时,我想通过使用数据表和会话将表单中的详细信息输入到网格视图中

<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();         


    }
4

2 回答 2

1

唯一的问题是你的GridView标记。您的部分代码在复制和验证相同的情况下工作。

如您所见<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>
于 2013-09-11T11:19:02.813 回答
-1

首先将数据插入数据库,然后通过gridview检索它......

于 2013-09-11T10:46:37.130 回答