如何从gridview中的TextBox获取数据?

时间:2016-12-19 05:59:15

标签: c# asp.net

这是我遇到问题的代码,我在上一页没有任何问题,但是在本页和后续页面中都会出现这种情况。

<asp:GridView ID="gv_employee" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false" OnRowEditing="Edit" OnRowCancelingEdit="CancelEdit" OnRowUpdating="Update" Width="1070px"
                            AlternatingRowStyle-BackColor="WhiteSmoke" HeaderStyle-BackColor="#C5D9F1">
                            <Columns>

                                <asp:TemplateField HeaderText="S.No" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="60px">
                                    <ItemTemplate>
                                        <%#Container.DataItemIndex+1 %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Employee Code" ItemStyle-Width="160px">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_gv_empcode" runat="server" Text='<%#Eval("employeecode")%>' />
                                    </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Employee Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_gv_empname" runat="server" Text='<%#Eval("employeename") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_gv_empname" runat="server" Text='<%#Eval("employeename") %>' />
                                    </EditItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Address1">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_gv_addr1" runat="server" Text='<%#Eval("address1") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_gv_addr1" runat="server" Text='<%#Eval("address1") %>' />
                                    </EditItemTemplate>
                                </asp:TemplateField>

<%--                                <asp:TemplateField HeaderText="Address2">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_gv_addr2" runat="server" Text='<%#Eval("address2") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_gv_addr2" runat="server" Text='<%#Eval("address2") %>' />
                                    </EditItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Address3">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_gv_addr3" runat="server" Text='<%#Eval("address3") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_gv_addr3" runat="server" Text='<%#Eval("address3") %>' />
                                    </EditItemTemplate>
                                </asp:TemplateField>


                                <asp:TemplateField HeaderText="City">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_gv_city" runat="server" Text='<%#Eval("city")%>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_gv_city" runat="server" Text='<%#Eval("city")%>' />
                                    </EditItemTemplate>
                                </asp:TemplateField>--%>


                                <asp:TemplateField HeaderText="Email">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_gv_email" runat="server" Text='<%#Eval("email") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_gv_email" runat="server" Text='<%#Eval("email") %>' />
                                    </EditItemTemplate>
                                </asp:TemplateField>


                                <asp:TemplateField HeaderText="Phone">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_gv_phone" runat="server" Text='<%#Eval("phone") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_gv_phone" runat="server" Text='<%#Eval("phone") %>' />
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:CommandField ShowEditButton="true" />
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="btndel" runat="server" Text="Delete" CommandArgument='<%#Eval("employeecode") %>'
                                            OnClientClick="return confirm('Do you want to delete?')" OnClick="Delete" />
                                    </ItemTemplate>
                                </asp:TemplateField>

                            </Columns>

                        </asp:GridView>

这是我的asp代码

public void BindData()
{
    string ls_sqlcmd = "select * from employeemst";
    SqlCommand cmd = new SqlCommand(ls_sqlcmd);
    gv_employee.DataSource = add.GetData(cmd);
    gv_employee.DataBind();
}


protected void Delete(object sender, EventArgs e)
{
    LinkButton lnk_deletestate = (LinkButton)sender;
    string str = "delete from employeemst where employeecode='" + lnk_deletestate.CommandArgument + "'";
    add.adddata(str);
    BindData();
}

protected void Update(object sender, GridViewUpdateEventArgs e)
{

    String emplname = ((TextBox)gv_employee.Rows[e.RowIndex].FindControl("txt_gv_empname")).Text;  
    String Addr1= ((TextBox)gv_employee.Rows[e.RowIndex].FindControl("txt_gv_addr1")).Text;  
    String addr2 = ((TextBox)gv_employee.Rows[e.RowIndex].FindControl("txt_gv_addr2")).Text;  
    String addr3 = ((TextBox)gv_employee.Rows[e.RowIndex].FindControl("txt_gv_addr3")).Text;  

    gv_employee.EditIndex = -1;
    BindData();
}

protected void Edit(object sender, GridViewEditEventArgs e)
{
    gv_employee.EditIndex = e.NewEditIndex;
    BindData();
}

protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
    gv_employee.EditIndex = -1;
    BindData();
}

}

这是我的aspx.cs代码。

我的问题是我无法从gridview <EditItemTemplate></EditItemTemplate>

中的文本框中获取数据

我没有在字符串emplnameaddr1addr2addr3中获得更新值。

1 个答案:

答案 0 :(得分:0)

你的问题是你在每个回发中绑定网格视图,在IsPostBack检查中移动你的BindData方法,然后你就可以得到数据

protected void Page_Load(object sender, EventArgs e)
{

   Session["page"] = "Employee";
    if (!IsPostBack)
    {
      BindData();

      DataTable dt = add.retrive("select * from statemst");

      for (int i = 0; i < dt.Rows.Count; i++)
      {

              ListItem item = new ListItem();
              item.Text = dt.Rows[i]["statedesc"].ToString();
              item.Value = dt.Rows[i]["statecode"].ToString();
              ddl_state.Items.Add(item);
       } 
    }

}