Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a hidden field in a gridview.

C#
<blockquote class="FQ"><div class="FQA">Quote:</div><asp:GridView ID="gridView" runat="server" CellPadding="5" GridLines="None" 
                  AllowPaging="false" AllowSorting="True" AutoGenerateColumns="false"
                OnRowEditing="OnRowEditing" OnRowDataBound="OnRowDataBound"  
                  CausesValidation="true" CssClass="gridview" BorderColor="AntiqueWhite" 
                  BorderWidth="0px" CellSpacing="1">
                <SelectedRowStyle CssClass="gridview-selected-row" />
                <HeaderStyle CssClass="gridview-header" Font-Bold="false" />
                <AlternatingRowStyle CssClass="gridview-alternate-row" />
                <RowStyle CssClass="gridview-row" />
            
                <Columns>
                                
                    <asp:TemplateField HeaderText="CustomerId" Visible="false">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# 
Bind("cust_id") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# 
Bind("cust_id") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                   <asp:TemplateField HeaderText="CustomerName" Visible="false">
                       <%--  <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# 
Bind("cust_name") %>'></asp:TextBox>
                        </EditItemTemplate>--%>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# 
Bind("cust_name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Display Name">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("cust_display_name") %>'></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredField2" runat="server" ControlToValidate="TextBox3" ErrorMessage ="Enter Display name" Display="Dynamic" ForeColor="Red" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# 
Bind("cust_display_name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                   
                   
                    <asp:TemplateField HeaderText="Email">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("cust_email") %>'></asp:TextBox>
                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox4" Display="Dynamic" ValidationExpression = "^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$" ErrorMessage="Invalid Email Id" ForeColor="Red"/>
                            <asp:RequiredFieldValidator ID="RequiredField1" runat="server" ControlToValidate="TextBox4" ErrorMessage ="Enter Email ID" Display="Dynamic" ForeColor="Red" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# 
Bind("cust_email") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Services Id" Visible="false">
                      <%--  <EditItemTemplate>
                            <asp:TextBox ID="TextBox7" runat="server" Text='<%# 
Bind("service_id") %>'></asp:TextBox>
                        </EditItemTemplate>--%>
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# 
Bind("service_id") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Services">
                    <InsertItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# 
Bind("service_display_name") %>'></asp:Label>
                    </InsertItemTemplate>
                   <%--     <EditItemTemplate>
                            <asp:TextBox ID="TextBox8" runat="server" Text='<%# 
Bind("service_display_name") %>'></asp:TextBox>--
                        </EditItemTemplate>--%>
                        <ItemTemplate>
                            <asp:Label ID="Label8" runat="server" Text='<%# 
Bind("service_display_name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Status">
                     <InsertItemTemplate>
                    <asp:Label ID="LabelCustStatus" runat="server" Text='<%# 
Bind("cust_status") %>'></asp:Label>
                    </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbl_Cust_Status" runat="server" Text='<%# 
Bind("cust_status") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Spoc Name">
                   <InsertItemTemplate>
                    <asp:Label ID="LabelCustSpoc" runat="server" Text='<%# 
Bind("cust_spoc") %>'></asp:Label>
                    </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbl_cust_spoc" runat="server" Text='<%# 
Bind("cust_spoc") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Address">
                   <InsertItemTemplate>
                    <asp:Label ID="LabelCustAddress" runat="server" Text='<%# 
Bind("cust_address") %>'></asp:Label>
                    </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbl_cust_address" runat="server" Text='<%# 
Bind("cust_address") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Is Parent" Visible="false">
                  <%--      <EditItemTemplate>
                            <asp:TextBox ID="TextBox9" runat="server" Text='<%# 
Bind("is_parent") %>'></asp:TextBox>
                        </EditItemTemplate>--%>
                        <ItemTemplate>
                            <asp:Label ID="Label9" runat="server" Text='<%# 
Bind("is_parent") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                     <asp:TemplateField HeaderText="Edit Services" >
                      <ItemTemplate>
                           <asp:LinkButton ID="lnkButtonAssignServ" runat="server" Autopostback="true" CauseValidation="true" OnClick="btnAssignServ_Click"
                            Text="Edit Services" 
                           ></asp:LinkButton>
                           </ItemTemplate>
                      </asp:TemplateField>

                   <asp:TemplateField>
                        <EditItemTemplate>
                           <asp:LinkButton ID ="lnkUpdate" ToolTip="Update" runat="server" OnClick="onUpdate" CssClass="update-link"></asp:LinkButton>
                           <asp:LinkButton ID ="LinkCancel" ToolTip="Cancel" runat="server" CssClass="cancel-link" CausesValidation="false" OnClick="onCancel"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                           <asp:LinkButton ID="LinkDelete" ToolTip="Delete" runat="server" CauseValidation="false" Autopostback="true" OnClick="onDelete"
                            OnClientClick="return confirm('Do you really want to delete it?')" CommandName="onDelete" CssClass="delete-link"></asp:LinkButton>
                           <asp:LinkButton ID="lnkButtonEdit" runat="server" CauseValidation="false"
                           CommandName="Edit" CssClass="edit-link" ToolTip="Edit"></asp:LinkButton>                            
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                </Columns>
            </asp:GridView>


When I tried to access Session['cust_id'] it in my code, it is showing me an error saying null value.

C#
protected void btnAssignServ_Click(object sender, EventArgs e)
        {
            GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
            gridView.SelectedIndex = row.RowIndex;
            TextBox cust_id = (TextBox)(gridView.Rows[gridView.SelectedIndex].FindControl("TextBox1"));
            Session["custid"] = cust_id.Text;
            Label displayName = (Label)(gridView.Rows[gridView.SelectedIndex].FindControl("Label2"));
            Session["Display_Name"] = displayName.Text;
            Label lblcustSpoc = (Label)(gridView.Rows[gridView.SelectedIndex].FindControl("lbl_cust_spoc"));
            Session["cust_spoc"] = lblcustSpoc.Text;
            Response.Redirect("AssignServToCustomer.aspx");
           
        }



Please help.
Posted
Updated 28-Jan-22 20:23pm
v2
Comments
CodeBlack 9-Oct-13 2:04am    
can you show me your whole aspx code for grid ? and where is your hiddent field in grid ? I cannot see it.
srmohanr 9-Oct-13 2:10am    
HeaderText="CustomerId", this is the hidden field
CodeBlack 9-Oct-13 2:24am    
<asp:TemplateField HeaderText="CustomerId" Visible="false">
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("cust_id") %>'>

<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("cust_id") %>'>



why do you keep this column visible='false' ? are you displaying that column again to visible ?
srmohanr 9-Oct-13 2:25am    
This is an user page. I don't want to show the user ID to customer.
Thanks7872 9-Oct-13 2:43am    
What do you mean by hidden field?

Do not bind column which needs to be kept hidden for user. Instead of that you can use hidden field and you can get value of the hidden field as shown in below sample :

ASPX Code:
XML
<asp:GridView ID="gridMain" runat="server" AutoGenerateColumns="False" >
    <Columns>
        <asp:TemplateField HeaderText="EmployeeName">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
                <input type="hidden" runat="server" id="hiddenEmployeeId" value='<%#Eval("EmployeeID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit Services">
            <ItemTemplate>
                <asp:LinkButton ID="lnkButtonAssignServ" runat="server" Autopostback="true" CauseValidation="true" OnClick="btnAssignServ_Click"
                    Text="Edit Services"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>



Code Behind :
C#
protected void btnAssignServ_Click(object sender, EventArgs e)
        {
            GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
            gridMain.SelectedIndex = row.RowIndex;
            var customerId = gridMain.Rows[gridMain.SelectedIndex].FindControl("hiddenEmployeeId") as HtmlInputHidden;

            if (customerId != null)
            {
                Session["custid"] = customerId.Value;
            }
        }
 
Share this answer
 
v2
You can also try the following to hide a field and get back into server side:
<ItemTemplate>
	<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("cust_id") %>' />
	<asp:Label ID ="CustomerId" runat ="server" Text =''></asp:Label>
</ItemTemplate>

Server Side Code:
string strValue=((HiddenField)Gridivew1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900