gpt4 book ai didi

c# - 在 Gridview 中设置和检索 HiddenField 的值

转载 作者:行者123 更新时间:2023-12-02 08:37:52 25 4
gpt4 key购买 nike

我有一个嵌入到 GridView 中的自定义控件。该控件需要一个值(来自 SqlDataSource1 的 Id 列),该值包含在 gridview 为其构造函数绑定(bind)到的同一数据表中。

我遇到的问题是我不明白如何设置隐藏字段的值。该字段的数据类型是 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>

隐藏字段:

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();

关于c# - 在 Gridview 中设置和检索 HiddenField 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19467310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com