在网格视图中设置和检索隐藏字段的值
本文关键字:隐藏 字段 检索 网格 视图 设置 | 更新日期: 2023-09-27 18:36:06
我有一个嵌入在GridView
中的自定义控件。 控件需要一个值(来自 SqlDataSource1 的 Id 列),该值包含在网格视图为其构造函数绑定到的同一数据表中。
我遇到的问题是我不明白如何设置隐藏字段的值。 此字段的数据类型为 Guid。
ASPX:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GridUserControl.ascx.cs" Inherits="CustomNavi_V1_ControlTest.CustomControls.GridUserControl" %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:srsfwa1_scrambledConnectionString %>" SelectCommand="SELECT [ApplicantId], [ValleyId], [SchoolId], [StatusId], [StudentID], [InterviewDate], [Id] FROM [tblSubmissions]"></asp:SqlDataSource>
<asp:GridView ID="StudentGridView" DataKeysNames="Submission" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnDataBound ="StudentGridView_OnDataBind">
<Columns>
<asp:BoundField DataField="ApplicantId" HeaderText="ApplicantId" SortExpression="ApplicantId" />
<asp:BoundField DataField="ValleyId" HeaderText="ValleyId" SortExpression="ValleyId" />
<asp:BoundField DataField="SchoolId" HeaderText="SchoolId" SortExpression="SchoolId" />
<asp:BoundField DataField="StudentID" HeaderText="StudentID" SortExpression="StudentID" />
<asp:BoundField DataField="InterviewDate" HeaderText="InterviewDate" SortExpression="InterviewDate" />
<asp:BoundField DataField="StatusId" HeaderText="StatusId" SortExpression="StatusId" visible="true"/>
<asp:BoundField DataField="Id" Visible ="false" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenField" DataField="SubmissionID" runat="server" Value=""/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText="Ed's Fabulous Custom Control!!" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
HiddenField
:
protected void AddCustomControl()
{
int iterator = 0;
foreach (GridViewRow row in StudentGridView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//StudentGridView.HiddenField.value= StudentGridView.
//HiddenField hiddenField = this.StudentGridView1.FindControl("
Guid submissionIdGuid = (Guid)hiddenField.Value; // ("SubmissionID").Value;
string controlID = "NavigatorControlID" + iterator.ToString();
string submissionID = row.Cells[6].Text; // tblSubmissions Id field
string statusId = row.Cells[5].Text; // tblSubmissions StatusId field
connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["srsfwa1_scrambledConnectionString"].ToString());
CustomNavi_V1._1.CustomNavi_V1 navigator = new CustomNavi_V1._1.CustomNavi_V1(submissionID, statusId);
navigator.ID = controlID;
navigator.SubmissionID = submissionIdGuid;
navigator.BackButtonImage = "Images/back_button.png";
navigator.ForwardButtonImage = "Images/fwd_button.png";
row.Cells[7].Controls.Add(navigator);
iterator++;
}
}
}
若要从隐藏字段获取 GUID,它将如下所示:
HiddenField hiddenField = (HiddenField)row.FindControl("HiddenField");
Guid submissionIdGuid = new Guid();
Guid.TryParse(hiddenField.Value, out submissionIdGuid );
并设置,
hiddenField.Value = submissionIdGuid.ToString();