locked
getting data of datagrid template textbox RRS feed

  • Question

  • User-1537000078 posted

    hi everybody

    i have textbox in a datagrid template and i want to read each of textbox data and save to database by clicking the button not in a datagrid under the datagrid. what i must to do?

    Friday, December 17, 2010 12:29 PM

Answers

  • User1992938117 posted

    use code as below on button click:

    protected void Button1_Click(object sender, EventArgs e)
        {
            for (int count = 0; count < GridView1.Rows.Count; count++)
            {
    
                //to get the template field value 
                TextBox tx1 = (TextBox)GridView1.Rows[count].FindControl("TextBox0");
                string str1 = tx1.Text;
                // here use query to save in data base. as you have current row textbox value in str1
            }
        }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 22, 2010 2:10 PM

All replies

  • User1266497125 posted

    You must loop through the rows of your data grid and read the control from the cell that contains your data. For example:

     for (int i = 0; i < gridView.Rows.Count; i++)
                {
                    GridViewRow row = gridView.Rows[i];
    
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox txt = row.Cells[4].Controls[1] as TextBox;
                        if (txt != null)
                        {
                             DoSomething(txt.Text);
                        }
                    }
                }


    Cell is the cell number where your text box is located, but Controls should be 1, because Controls[0] contains Literal value.

    Hope this helps,

    Huske

    Friday, December 17, 2010 2:44 PM
  • User-1537000078 posted

    i use this code and give me this error:

    Specified argument was out of the range of valid values.
    Parameter name: index

    from this line:

    TextBox txt = row.Cells[1].Controls[1] as TextBox;

    this is my gridview:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        EnableModelValidation="True">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <table style="border: 2px ridge #F4F4F4; width: 100%">
                                        <tr>
                                            <td colspan="3">
                                                                         
                                                <asp:Label ID="Label34" runat="server" Font-Bold="True" Font-Size="Large" 
                                                    Text="حذف کتاب"></asp:Label>
                                                   
                                                <asp:ImageButton ID="delBtn22" runat="server" 
                                                    CommandArgument='<%# Eval("ID") %>' Height="26px" 
                                                    ImageUrl="~/graghic/button/bullet_delete.png" oncommand="delBtn_Command" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">
                                                            
                                                <asp:Label ID="Label7" runat="server" Font-Bold="True" Font-Size="X-Large" 
                                                    ForeColor="#24B8FF" Text='<%# Eval("nameketab") %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td rowspan="5" width="150">
                                                <asp:Image ID="Image1" runat="server" BorderStyle="Solid" BorderWidth="2px" 
                                                    Height="120px" Width="100px" />
                                            </td>
                                            <td bgcolor="#DDDDDD" width="200">
                                                   
                                                <asp:Label ID="Label12" runat="server" Font-Bold="True" Font-Size="Large" 
                                                    Text="نویسنده"></asp:Label>
                                            </td>
                                            <td bgcolor="#DDDDDD" width="200">
                                                <asp:Label ID="Label1" runat="server" Font-Size="Medium" 
                                                    Text='<%# Eval("nevisande") %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width="200">
                                                   
                                                <asp:Label ID="Label13" runat="server" Font-Bold="True" Font-Size="Large" 
                                                    Text="مترجم"></asp:Label>
                                            </td>
                                            <td width="200">
                                                <asp:Label ID="Label2" runat="server" Font-Size="Medium" 
                                                    Text='<%# Eval("motarjem") %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td bgcolor="#DDDDDD" width="200">
                                                   
                                                <asp:Label ID="Label4" runat="server" Font-Bold="True" Font-Size="Large" 
                                                    Text="ناشر"></asp:Label>
                                            </td>
                                            <td bgcolor="#DDDDDD" width="200">
                                                <asp:Label ID="Label3" runat="server" Font-Size="Medium" 
                                                    Text='<%# Eval("nasher") %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width="200">
                                                   
                                                <asp:Label ID="Label5" runat="server" Font-Bold="True" Font-Size="Large" 
                                                    Text="قیمت"></asp:Label>
                                            </td>
                                            <td width="200">
                                                <asp:Label ID="Label10" runat="server" Font-Size="Medium" 
                                                    Text='<%# Eval("gheymat") %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                                                <asp:Label ID="Label14" runat="server" 
                                                    Font-Bold="True" Font-Size="Large" Text="تعداد"></asp:Label>
                                                 
                                                <asp:TextBox ID="TextBox0" runat="server" Text='<%# Eval("tedad") %>' 
                                                    Width="35px" Wrap="False"></asp:TextBox>
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>



    Saturday, December 18, 2010 2:15 PM
  • User1266497125 posted

    In that case try using row[i].Cells[1].FindControl("control id that you specified"). THis will return a Control which you need to cast into text box or something.

    Sunday, December 19, 2010 5:47 AM
  • User-1537000078 posted

    it give that error again

    Monday, December 20, 2010 2:21 AM
  • User-1537000078 posted

    plz sm help

    i just want to read each data in a textbox of datagrid that use template. what is the rule?

    Tuesday, December 21, 2010 12:24 PM
  • User1992938117 posted

    use code as below on button click:

    protected void Button1_Click(object sender, EventArgs e)
        {
            for (int count = 0; count < GridView1.Rows.Count; count++)
            {
    
                //to get the template field value 
                TextBox tx1 = (TextBox)GridView1.Rows[count].FindControl("TextBox0");
                string str1 = tx1.Text;
                // here use query to save in data base. as you have current row textbox value in str1
            }
        }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 22, 2010 2:10 PM