在Gridview中设置和检索隐藏字段中的值

时间:2013-10-19 14:48:29

标签: c# asp.net gridview hidden-field

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

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

1 个答案:

答案 0 :(得分:6)

要从隐藏字段获取GUID,它将类似于:

HiddenField hiddenField = (HiddenField)row.FindControl("HiddenField");
Guid submissionIdGuid =  new Guid();
Guid.TryParse(hiddenField.Value, out submissionIdGuid );

并设置,

hiddenField.Value = submissionIdGuid.ToString();