Advertisement

02.13.2008 at 05:39AM PST, ID: 23159497
[x]
Attachment Details

Hidden fields under ItemTemlate" do not render correctly.

Asked by KLRDER in .NET Framework 2.0, Programming for ASP.NET, Visual Studio .NET 2005

Tags: C#, ie 6

I have a gridview with an asp:TemplateField containing an ItemTemplate.  Inside this ItemTemplate I am trying to embed some data that comes from the database so that the user never sees it, but I have access to it programatically when I need it.  I have tryed using asp:HiddenField and i have tried using span and asp:literal, setting them to invisible.  But they don't render correctly as do all the other controls in the templates.  This is the only field I'm having troubl. with.

When I do TableRow.FindControl("X") it always returns null where X is the id of the control I'm using.

I have attached a code snippet.  Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
Here is the grid:
 
        <asp:GridView ID="examplegrid" runat="server" AutoGenerateColumns="false" 
            Height="550px" Width="100%">
            <AlternatingRowStyle BackColor="AliceBlue" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle />
            <EditRowStyle BackColor="#999999" />
            <Columns>
                <asp:BoundField DataField="Trip_Detail_ID" HeaderText="Trip Nr" />
                <asp:TemplateField HeaderText="Origin">
                    <itemtemplate>
                        <span style="text-transform:capitalize;"><%#DataBinder.Eval(Container.DataItem, "Orig_Cust_Loc_NM") %></span>
                    </itemtemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Destination">
                    <itemtemplate>
                        <span style="text-transform:capitalize;"><%#DataBinder.Eval(Container.DataItem, "Dest_Cust_Loc_NM") %></span>
                    </itemtemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Leg_Type_DS" HeaderText="Leg Type" />
                <asp:BoundField DataField="View_DT" HeaderText="View DT" />
                <asp:BoundField DataField="Inld_Request_Status_DS" HeaderText="Leg Status" />
                <asp:BoundField DataField="Approver_AN" HeaderText="Approver" />
                <asp:BoundField DataField="Leg_Start_DT" HeaderText="Leg Start" />
                <asp:BoundField DataField="Leg_End_DT" HeaderText="Leg End" />
                <asp:TemplateField>
                    <headertemplate>
                    Select
                    </headertemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <itemtemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" />
                    </itemtemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HiddenField ID="hfTripLegId" value="<%#DataBinder.Eval(Container.DataItem,"Trip_Leg_ID")%>" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
 
The last template with the hidden field is not getting rendered to the browser correctly.  It is rendered as:
<td>
    <asp:HiddenField ID="hfTripLegId" value="14408201" runat="server" />
</td>
 
Notice it got rendered as an asp:HiddenField instead of an input control with the type of hidden.
 
In fact, it looks like anything I try to make hidden in this cell is rendered verbatim in the ASPX file instead of rendering the html version of it.
 
When I use code like this:
 
            foreach (TableRow tr in examplegrid.Rows)
            {
                // find the checkbox in the row.
                chk = (CheckBox)tr.Cells[9].FindControl("chkSelect");
                if (chk.Checked == true)
                {
                    // Get the trip_leg_id
 
                    HiddenField hid = (HiddenField)tr.Cells[10].FindControl("hfTripLegId");
                    tripLegId = hid.Value;                    
                    // Do Accept.
                    business.AcceptTrip(tripLegId);
                }
            }
The checkbox is found in the cells.  So I know I'm using the correct syntax.  But the hidden field is not.  I've even tried "tr.FindControl("hfTripLegId) and it came out null.
[+][-]02.13.2008 at 05:43AM PST, ID: 20884045

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.13.2008 at 06:18PM PST, ID: 20890385

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .NET Framework 2.0, Programming for ASP.NET, Visual Studio .NET 2005
Tags: C#, ie 6
Sign Up Now!
Solution Provided By: Arlowin
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628