Gridview中的隐藏字段

时间:2015-03-05 03:53:40

标签: c# asp.net

我在gridview中有隐藏字段。我没有得到代码背后的价值。
没有MasterPage和Ajax,它可以正常工作,你能帮助我获得代码背后的价值。
谢谢

protected void empgrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateEmployee")
            {

                int index = Convert.ToInt32(e.CommandArgument);

                GridViewRow row = empgrid.Rows[index];

                Session["Employee"] = ((HiddenField)empgrid.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("EmployeeID")).Value;


                Response.Redirect("UpdateEmployee.aspx");

            }

        }

<%@ Page  EnableEventValidation = "false"  Title=""  MasterPageFile="~/EmploymentWeb.Master" Language="C#" AutoEventWireup="true" CodeBehind="employeeborowes.aspx.cs" Inherits="Employment_Site.employeeborowes" %>
<asp:Content ID="Content1" ContentPlaceHolderID="AllContent" runat="server">

    <table style="width: 100%">
        <tr>
            <td style="width: 105px">
                <asp:Label ID="Label1" runat="server" Text="Employee Name"></asp:Label>
            </td>
            <td style="width: 178px">
                <asp:TextBox ID="empname" runat="server" Width="165px"></asp:TextBox>
            </td>
            <td class="style2" style="width: 61px">
                <asp:Button ID="btnsearch" runat="server" Text="Search" Width="70px" 
                    onclick="btnsearch_Click" />
            </td>
            <td class="style2" style="width: 72px">
                <asp:Button ID="btnclear" runat="server" onclick="btnclear_Click" Text="Clear" 
                    Width="70px" />
            </td>
            <td style="width: 144px">
                &nbsp;</td>
        </tr>
        <tr>
            <td style="width: 105px">
                &nbsp;</td>
            <td colspan="4">
             <asp:UpdatePanel ID="UpdatePanel1" runat="server"
 UpdateMode="Conditional"  >
 <ContentTemplate>

                <asp:GridView ID="empgrid" runat="server" AutoGenerateColumns="False" 
                    CellPadding="4" ForeColor="#333333" GridLines="None" Width="303px" 
                    onrowcommand="empgrid_RowCommand">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:TemplateField Visible="False">

                            <ItemTemplate>
                             <asp:HiddenField ID="EmployeeID" runat="server" Value='<%# Eval("EmployeeID") %>'  />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                        <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                        <asp:TemplateField>

                            <ItemTemplate>

                                <asp:Button ID="btnupdate" runat="server" Text="Update" 
                                CommandName="UpdateEmployee" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>

                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" 
                        HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" ForeColor="#333333" Font-Bold="True" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>

                </ContentTemplate>
                  </asp:UpdatePanel>
            </td>
        </tr>
        <tr>
            <td style="width: 105px">
                &nbsp;</td>
            <td style="width: 178px">
                &nbsp;</td>
            <td class="style2" style="width: 61px">
                &nbsp;</td>
            <td class="style2" style="width: 72px">
                &nbsp;</td>
            <td style="width: 144px">
                &nbsp;</td>
        </tr>
        <tr>
            <td style="width: 105px">
                &nbsp;</td>
            <td style="width: 178px">
                &nbsp;</td>
            <td class="style2" style="width: 61px">
                &nbsp;</td>
            <td class="style2" style="width: 72px">
                &nbsp;</td>
            <td style="width: 144px">
                &nbsp;</td>
        </tr>
    </table>

   </asp:Content>

2 个答案:

答案 0 :(得分:0)

试试这个

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
HiddenField empID = (HiddenField)row.FindControl("EmployeeID");
Session["Employee"] = empID.Value;

答案 1 :(得分:0)

试试这个:    使用Response.Redirect(&#34; UpdateEmployee.aspx&#34;,false); 而不是Response.Redirect(&#34; UpdateEmployee.aspx&#34;);

这是因为Redirect和会话变量的工作方式。 创建新会话时,会在包含会话令牌的客户端上设置volatile cookie。在所有后续请求中,只要服务器会话和客户端cookie未过期,ASP.NET就可以查看此cookie并找到正确的会话。

重定向的作用是向客户端发送特殊标头,以便它向服务器请求与其等待的页面不同的页面。

希望这会有所帮助!!