2013-10-19 71 views
2

我有一個嵌入在GridView中的自定義控件。該控件需要包含在GridView綁定到其構造函數的同一數據表中的值(來自SqlDataSource1的Id列)。在Gridview中從HiddenField中設置和檢索值

我遇到的問題是我不明白如何設置隱藏字段的值。該字段的數據類型是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++; 
     } 
    } 
} 
+0

我已將aspxGridView標記更改爲GridView標記,因爲在標記中它看起來不像aspxgridview。 – afzalulh

回答

6

要隱藏字段得到GUID,這將是這樣的:

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

而設置,

hiddenField.Value = submissionIdGuid.ToString(); 
+0

感謝您的指導!得到它像冠軍一樣工作。 :) – deadEddie

相關問題