Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem when working on gridview which is binded in a datalist.
I am building a input form in a matrix from which is depend on two tables.
on table for rows and other table for columns.
like this:
------------------------------------------------------------------
------------ |-- Col1--| ---Col2--- | ---Col3--- | --Col4---
--------------------------------------------------------------------
Rowname1 | Textbox | Textbox | Textbox | Textbox
------------------------------------------------------------------
Rowname2 | Textbox | Textbox | Textbox | Textbox
------------------------------------------------------------------
Rowname3 | Textbox | Textbox | Textbox | Textbox
------------------------------------------------------------------
Rowname4 | Textbox | Textbox | Textbox | Textbox
------------------------------------------------------------------
Rowname5 | Textbox | Textbox | Textbox | Textbox
------------------------------------------------------------------


this is my code for building this layout

XML
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"
           Width="490px" RepeatDirection="Horizontal" BorderWidth="1px">
           <ItemTemplate>
               <table>
                   <tr>
                       <td>
                           <table border="1px">
                               <tr>
                                   <td style="text-align: center">
                                       <asp:Label ID="Label1" CssClass="floa" runat="server" Text='<%#Eval("Branch_name") %>'></asp:Label>
                                                                          </td>
                               </tr>
                               <tr>
                                   <td>
                                       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="false"
                                           BorderStyle="None">
                                           <Columns>
                                               <asp:TemplateField HeaderText="">
                                                   <ItemTemplate>
                                                       <asp:Label ID="Label3" runat="server" Text='<%#Eval("Affiliate") %>' Width="150px"></asp:Label>
                                                   </ItemTemplate>
                                               </asp:TemplateField>
                                               <asp:TemplateField HeaderText="">
                                                   <ItemTemplate>
                                                       <asp:CheckBox ID="CheckBox1" runat="server" />
                                                   </ItemTemplate>
                                               </asp:TemplateField>
                                               <asp:TemplateField HeaderText="Value(%)">
                                                   <ItemTemplate>
                                                       <asp:TextBox ID="TextBox3" runat="server" CssClass="text"></asp:TextBox>
                                                   </ItemTemplate>
                                               </asp:TemplateField>
                                           </Columns>
                                       </asp:GridView>
                                   </td>
                               </tr>
                           </table>
                       </td>
                   </tr>
               </table>
           </ItemTemplate>
       </asp:DataList>



I am using Gidview for input which is binded in datalist horizontly

This is my code for binding this gridview inside datalist:--

C#
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            GridView gv = (GridView)e.Item.FindControl("GridView1");

            if (gv != null)
            {
                gv.DataSource = s.Tables[1];
                gv.DataBind();
                if (i != 1)
                {
                    gv.Columns[0].Visible = false;
                }
                i++;
         }


The binding of this gridview inside this control is working properly; but
when i want to fetch data from the textbox of gridview control i can't get it.
means I successfuly find the control on submit button's click. But i can't able to find the edited changed data from the textbox from gridview control.

this is my coding for submit button's click
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        GridView gv = (GridView)DataList1.Items[0].Controls[1].FindControl("GridView1");
        int i = gv.Rows.Count;
        string t = ((TextBox)gv.Rows[1].FindControl("DataList1_ctl00_GridView1_ctl03_TextBox3")).Text;
        Response.Write(t);

}


Please, tel me if i am wrong anywhere, and how can i fetch data from Gridview's Textbox
Posted

1 solution

you can find from gridview or datalist nested control with its server side id not client-side id. change your id in findcontrol like this

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        GridView gv = (GridView)DataList1.Items[0].Controls[1].FindControl("GridView1");
        int i = gv.Rows.Count;
        string t = ((TextBox)gv.Rows[1].FindControl("TextBox3")).Text;
        Response.Write(t);

}
 
Share this answer
 
Comments
Mohd Imran Saifi 17-Sep-12 4:00am    
thanks; I forgot to check this.

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