2

我有一个嵌入在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++;
        }
    }
}
4

1 回答 1

6

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

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

并设置,

hiddenField.Value = submissionIdGuid.ToString();
于 2013-10-19T16:07:13.000 回答