2011-06-03 49 views
0

編輯:我從第一個答覆中指出了在hf中添加。這有助於解決其他一些問題,但仍未解決問題。綁定到GridView的問題HiddenField

我想綁定一個列表到很多隱藏字段(我只使用50中的2個)用於臨時存儲的GridView,這樣用戶就可以創建和刪除多個項目,而不必前後往返數據庫在創建過程中多次。我很好地綁定並從BoundFields中調用;但是,當我試圖回憶隱藏的字段值時,它會返回爲空。我可能會遇到綁定到隱藏字段的問題。這裏是我的代碼

ASP:

<asp:GridView ID="grdOtherCarrier" runat="server" AutoGenerateColumns="False"> 
<Columns> 
<asp:BoundField HeaderText="Sequence" DataField="Sequence"></asp:BoundField> 
<asp:BoundField HeaderText="Pay ID" DataField="PayID"></asp:BoundField> 
<asp:BoundField HeaderText="Provider" DataField="Provider"></asp:BoundField> 
      <asp:TemplateField HeaderText="Actions"> 
       <ItemTemplate> 
        <asp:HiddenField ID="hfInsuredLastName" Value="<%#Eval("InsuredLastName")%>" runat="server" /> 
        <asp:HiddenField ID="hfInsuredFirstName" Value="<%#Eval("InsuredFirstName")%>" runat="server" /> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView> 

C#/代碼背後:

protected void lblSubmitOtherCarrier_Click(object sender, EventArgs e) 
    { 
     List<OtherCarrier> OCList = new List<OtherCarrier>(); 

     OtherCarrier OC; 

foreach (GridViewRow row in grdOtherCarrier.Rows) 
     { 
      if (row.RowType == DataControlRowType.DataRow) 
      { 
       OC = new OtherCarrier(); 
       String Sequence = row.Cells[0].Text.ToString() ; // primary value 
       String PayID = row.Cells[1].Text.ToString(); // primary value 
       String Provider = row.Cells[2].Text.ToString(); // primary value 

       HiddenField InsuredLastName = (HiddenField)row.FindControl("hfInsuredLastName"); 
       HiddenField InsuredFirstName = (HiddenField)row.FindControl("hfInsuredFirstName"); 

       OC.Sequence = Sequence; 
       OC.PayID = PayID; 
       OC.Provider = Provider; 
       OC.InsuredLastname = InsuredLastName.Value.ToString(); 
       OC.InsuredFirstName = InsuredFirstName.Value.ToString(); 

       OCList.Add(OC); 
      } 
     } 
     OC = new OtherCarrier(); 

     OC.Sequence = txtSequence.Text; 
     OC.PayID = txtPayID.Text; 
     OC.Provider = txtProvider.Text; 
     OC.InsuredLastname = txtInsuredLastname.Text; 
     OC.InsuredFirstName = txtInsuredFirstName.Text; 
     OCList.Add(OC); 

     TD.OtherCarrierList = OCList; 

     grdOtherCarrier.DataSource = OCList; 
     grdOtherCarrier.DataBind(); 

     mpeAddOtherCarrier.Hide(); 
     txtSequenceNo.Text = (grdOtherCarrier.Rows.Count + 1).ToString(); 
    } 

當它得到從隱藏字段分配OC.InsuredLastname隱藏字段快到了爲空值。要麼我沒有正確閱讀,要麼沒有正確書寫。

+0

您是否在客戶端填充隱藏的字段值? – 2011-06-05 17:47:03

回答

1

所以......

你正在努力尋找控制作爲

(HiddenField)row.FindControl("InsuredLastName"); 

,你可以看到,一個名爲InsuredLastName在HTML,當你明確的說,你的控件名稱是:

<asp:HiddenField 
    ID="hfInsuredLastName" 
    Value="<%#Eval("InsuredLastName")%>" 
    runat="server" /> 

命名hfInsuredLastName

所有你需要做的爲c將電話轉到

(HiddenField)row.FindControl("hfInsuredLastName") 
+0

我將hf添加到了字段中,並且通過將ID從HTML複製並粘貼到後面的代碼並確認它們具有正確的名稱,並且仍然遇到同樣的問題。我將更新上面的代碼以顯示此修復程序。 – Daver 2011-06-03 17:13:05