无法在RowCommand中获取gridview页脚值

时间:2015-10-09 04:06:34

标签: c# asp.net asp.net-mvc-4 gridview

我正在创建一个页面,在动态网格视图中输入员工休假详细信息。数据的输入将由gridview的页脚完成。当我尝试从页脚获取值时,所有文本值以及其他值都将变为null或“”。

以下是RowCommand的代码:

protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ADD"))
            {

                TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");
                RadioButtonList rblFooter = (RadioButtonList)gvEmployeeDetails.FooterRow.FindControl("rblFooter");
                TextBox txtAddDoj = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDoj");
                TextBox txtAddLeaves = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddLeaves");
                TextBox txtAddDesignation = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDesignation");

                conn.Open();
                string cmdstr = "insert into tblLeaves(EmpId,IsActive,DOJ,Leaves,Designation) values(@EmpId,@IsActive,@DOJ,@Leaves,@Designation)";
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.Parameters.AddWithValue("@EmpId", txtAddEmpID.Text);
                cmd.Parameters.AddWithValue("@IsActive", rblFooter.SelectedValue);
                cmd.Parameters.AddWithValue("@DOJ", txtAddDoj.Text);
                cmd.Parameters.AddWithValue("@Leaves", txtAddLeaves.Text);
                cmd.Parameters.AddWithValue("@Designation", txtAddDesignation.Text);
                cmd.ExecuteNonQuery();
                conn.Close();
                BindData();
            }
        }

以下是gridview:

<asp:GridView ID="gvEmployeeDetails" runat="server" Width="100%"
                    AutoGenerateColumns="False" ShowFooter="True"
                    onrowcommand="gvEmployeeDetails_RowCommand"
                    onrowdeleting="gvEmployeeDetails_RowDeleting"
                    onrowupdating="gvEmployeeDetails_RowUpdating"
                    onrowcancelingedit="gvEmployeeDetails_RowCancelingEdit"
                    onrowediting="gvEmployeeDetails_RowEditing" style="text-align: center" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
                <Columns>           
                    <asp:TemplateField HeaderText="Employee ID">
                        <FooterTemplate>
                            <asp:TextBox ID="txtAddEmpID" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvFooterEmpID" runat="server" ControlToValidate="txtAddEmpID" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "EmpId") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>           
                            <asp:Label ID="lblEditEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "EmpId") %>'></asp:Label>           
                        </EditItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Is Active">
                        <EditItemTemplate>
                            <asp:RadioButtonList ID="rblEdit" runat="server">
                                <asp:ListItem>Yes</asp:ListItem>
                                <asp:ListItem>No</asp:ListItem>
                            </asp:RadioButtonList>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:RadioButtonList ID="rblFooter" runat="server">
                                <asp:ListItem>Yes</asp:ListItem>
                                <asp:ListItem>No</asp:ListItem>
                            </asp:RadioButtonList>
                            <asp:RequiredFieldValidator ID="rfvFooterIsActive" runat="server" ControlToValidate="rblFooter" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblIsActive" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "IsActive") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Date Of Joining">
                        <ItemTemplate>
                            <asp:Label ID="lblDoj" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DOJ") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>           
                            <asp:TextBox ID="txtDoj" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DOJ") %>'></asp:TextBox>           
                            <ajaxToolkit:CalendarExtender ID="txtDoj_CalendarExtender" runat="server" BehaviorID="txtDoj_CalendarExtender" TargetControlID="txtDoj" />
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtAddDoj" runat="server" MaxLength="10" OnTextChanged="txtAddDoj_TextChanged" ></asp:TextBox>
                            <ajaxToolkit:CalendarExtender ID="txtAddDoj_CalendarExtender" runat="server" BehaviorID="txtAddDoj_CalendarExtender" TargetControlID="txtAddDoj" />
                            <asp:RequiredFieldValidator ID="rfvFooterDOJ" runat="server" ControlToValidate="txtAddDoj" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                        </FooterTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="No. Of Leaves">
                        <ItemTemplate>
                            <asp:Label ID="lblLeaves" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Leaves") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>           
                            <asp:TextBox ID="txtLeaves" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Leaves") %>'></asp:TextBox>           
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtAddLeaves" runat="server" ></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvFooterLeaves" runat="server" ControlToValidate="txtAddLeaves" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                        </FooterTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Designation">
                        <ItemTemplate>
                            <asp:Label ID="lblDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Designation") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>           
                            <asp:TextBox ID="txtDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Designation") %>'></asp:TextBox>           
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtAddDesignation" runat="server" ></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvFooterDesig" runat="server" ControlToValidate="txtAddDesignation" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                        </FooterTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Action">
                        <ItemTemplate>
                           <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/Edit.png" Height="32px" Width="32px"/>
                           <asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Delete.png" Height="32px" Width="32px"/>
                        </ItemTemplate>
                        <EditItemTemplate>
                           <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/icon-update.png"/>
                           <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-Cancel.png"/>
                        </EditItemTemplate>
                        <FooterTemplate>
                           <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" Width="100px"></asp:LinkButton>
                        </FooterTemplate>
                    </asp:TemplateField>                    
                </Columns>           
                    <FooterStyle BackColor="White" ForeColor="#000066" />
                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                    <RowStyle ForeColor="#000066" />
                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#007DBB" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#00547E" />
            </asp:GridView>

请在我出错的地方纠正我。

1 个答案:

答案 0 :(得分:2)

您未在查找控制代码行中使用发件人。尝试替换下面的行。

TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");

此。

protected void gridview_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
    if (e.CommandName == "Add") {
        TextBox txtAddEmpID = (TextBox)((GridView)sender).FooterRow.FindControl("txtAddEmpID");
    }
}