问题绑定到GridView HiddenField

时间:2011-06-03 16:07:52

标签: list gridview data-binding hiddenfield

编辑:我在第一次回复中添加了hf。这有助于解决其他一些问题,但仍未解决问题。

我正在尝试将列表绑定到gridview,其中包含大量隐藏字段(我将仅使用50个中的2个)进行临时保存,因此用户可以创建和删除多个项目而无需来回数据库在创作过程中多次。我很好地绑定并从BoundFields中回忆起来;但是,当我尝试回忆隐藏的字段值时,它会返回null。我可能遇到绑定到隐藏字段的问题。这是我的代码

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时,隐藏字段将显示为空值。要么我没有正确阅读,要么没有正确写入。

1 个答案:

答案 0 :(得分:1)

所以...

你正试图找到一个控件

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

正如您所看到的,当您在HTML中明确说出您的控件名称为<{p>}时,请将其命名为InsuredLastName

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

名为hfInsuredLastName

您需要做的就是将呼叫更改为

(HiddenField)row.FindControl("hfInsuredLastName")