网格视图中的隐藏字段
本文关键字:隐藏 字段 视图 网格 | 更新日期: 2024-10-18 15:17:39
我在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">
</td>
</tr>
<tr>
<td style="width: 105px">
</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">
</td>
<td style="width: 178px">
</td>
<td class="style2" style="width: 61px">
</td>
<td class="style2" style="width: 72px">
</td>
<td style="width: 144px">
</td>
</tr>
<tr>
<td style="width: 105px">
</td>
<td style="width: 178px">
</td>
<td class="style2" style="width: 61px">
</td>
<td class="style2" style="width: 72px">
</td>
<td style="width: 144px">
</td>
</tr>
</table>
</asp:Content>
试试这个
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
HiddenField empID = (HiddenField)row.FindControl("EmployeeID");
Session["Employee"] = empID.Value;
试试这个:使用响应。重定向("UpdateEmployee.aspx",false);而不是响应。重定向("UpdateEmployee.aspx");
这是因为重定向和会话变量的工作方式。创建新会话时,会在包含会话令牌的客户端上设置一个易失性cookie。在所有后续请求中,只要服务器会话和客户端cookie尚未过期,ASP。NET可以查看此cookie并找到正确的会话。
Redirect所做的是向客户端发送一个特殊的标头,以便它向服务器请求与等待的页面不同的页面。
希望这能有所帮助!!