Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i pass the value from text box to label in gridview

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                   ForeColor="#333333" GridLines="None" ShowFooter="True"
                   onselectedindexchanged="GridView1_SelectedIndexChanged"
                   onrowcommand="GridView1_RowCommand" >
                   <RowStyle BackColor="#EFF3FB" />
                   <Columns>
                       <asp:TemplateField  HeaderText="NAME">
                       <ItemTemplate>
                               <asp:TextBox ID="txtName" runat="server" Visible ="true" ></asp:TextBox>
                               <asp:Label ID="lblName" visible = "false"  runat="server"></asp:Label>

                       </ItemTemplate>

                       </asp:TemplateField>
                        <asp:TemplateField  HeaderText="AGE">
                       <ItemTemplate>
                               <asp:TextBox ID="txtAge" runat="server" Visible ="true" ></asp:TextBox>
                               <asp:Label ID="lblAge" visible = "false" runat="server"></asp:Label>

                       </ItemTemplate>

                       </asp:TemplateField>
                        <asp:TemplateField  HeaderText="Qualification">
                       <ItemTemplate>
                               <asp:TextBox ID="txtQual" runat="server" Visible ="true" ></asp:TextBox>
                               <asp:Label ID="lblQual" visible = "false" runat="server"></asp:Label>

                       </ItemTemplate>

                       </asp:TemplateField>
                        <asp:TemplateField  HeaderText="Experience">
                       <ItemTemplate>
                               <asp:TextBox ID="txtExp" runat="server" Visible ="true" ></asp:TextBox>
                               <asp:Label ID="lblExp" visible = "false" runat="server"></asp:Label>

                       </ItemTemplate>

                       </asp:TemplateField>
                       <asp:ButtonField HeaderText = "Add" text = "Add" CommandName = "Add"  ButtonType="Button" />

                         <%-- <EditItemTemplate>
                               <asp:TextBox ID="txtName" runat="server" Visible ="true" ></asp:TextBox>
                               <asp:Label ID="lblName" visible = "false" runat="server"></asp:Label>
                           </EditItemTemplate>--%>
                       <%--<asp:BoundField HeaderText="NAME" />
           <asp:BoundField HeaderText="AGE" />
           <asp:BoundField HeaderText="Qualification" />
           <asp:BoundField HeaderText="Experience" />
           <asp:BoundField />
           <asp:BoundField HeaderText="Add" />--%>
                   </Columns>
                   <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                   <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                   <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                   <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                   <EditRowStyle BackColor="#2461BF" />
                   <AlternatingRowStyle BackColor="White" />
               </asp:GridView>
Posted
Comments
Arul R Ece 9-Apr-12 2:28am    
For example i enter the value in txetx box.it should pass to lable.
Please look into it

Hi ,
Try this

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       foreach (GridViewRow row  in GridView1.Rows)
       {
           if ((Label)row.FindControl("lbl") is Label)
           {
               ((Label) row.FindControl("lbl")).Text = txtname.Text;
           }
       }

   }

Or
C#
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//for specific row
    ((Label) GridView1.Rows[e.NewSelectedIndex].FindControl("lbl")).Text = txtname.Text;
}

Best Regards
M.Mitwalli
 
Share this answer
 
v2
Hi,
U can do this using row databound event it will fire on binding every row.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx[^]

Best Luck
Happy Coding
 
Share this answer
 
Comments
Arul R Ece 9-Apr-12 2:39am    
Hi patil,

Thanks for your info.
but i am not able to get that answer
Nilesh Patil Kolhapur 9-Apr-12 2:42am    
just understand how rowdatabound event works .
in this event u can find both controls label and textbox. after
this u can get value of textbox and assign to label. its really simple
Please Provide me when you want to pass value of text box to label at a time of binding or at a time on click on row

if you want to Pass value at at a time of binding then you can Assign DB parameter to Text box as well as label same field.

or you can assign value at atime of Binding use Row bound Event of Grid View.


thanks
Anil Avhad
(India)
 
Share this answer
 
Comments
Arul R Ece 9-Apr-12 2:41am    
Hi Anil.
When i click the ADD button.
the text box value should to Label.

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