Assign Label value to dynamically generated ASP.Net GridView row on PageLoad using C#

Last Reply on Feb 27, 2018 02:36 AM By dharmendr

Posted on Feb 27, 2018 02:06 AM

Hello,

reference to my Pervious Question I have added one label insted of textbox1 and i want to load the label value at the page load. I tried this

dr["Column1"] = Label1.Text;

it's working after I clicked AddNewRowToGrid() function. How can I load in the page load label value. 

drCurrentRow["Column1"] = Label1.Text;

 

<div>
      <asp:gridview ID="Gridview1" runat="server" ShowFooter="true"
            AutoGenerateColumns="false" onrowcreated="Gridview1_RowCreated">
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
            <asp:TemplateField HeaderText="Header 1">
                <ItemTemplate>
                    <asp:Label ID="TextBox1" runat="server" Text=""></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header 2">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header 3">
                <ItemTemplate>
                     <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
                        onclick="ButtonAdd_Click" />
                </FooterTemplate>
            </asp:TemplateField>
                 <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Remove</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:gridview>
        <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text="name"></asp:Label>
    </div>

Thanks

Posted on Feb 27, 2018 02:36 AM
irshad1231 says:
<asp:Label ID="TextBox1" runat="server" Text=""></asp:Label>

 To add Label value in page load you have to assign the Text property to the GridView TextBox like below.

<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Column1") %>'></asp:TextBox>