Karim Tarek
Karim Tarek

Reputation: 401

Can't access the text in the TextArea in a gridview

<ItemTemplate>
<textarea ID="TextArea1" TextMode="multiline" runat="server" cols="20" name="S1" rows="2">         </textarea><br />
<asp:Button ID="Button1" runat="server" CommandName = "comment" Text = "Comment"/>
</ItemTemplate>

 SqlParameter par1 = new SqlParameter("@txt", SqlDbType.VarChar);
        par1.Direction = ParameterDirection.Input;
        par1.Value = Request.Form["TextArea1"];
        com.Parameters.Add(par1);

I can't get the text in a textArea which is in a gridview. I just can't access it from the behind code.

I want to assign the text in the textArea to a variable but I can't reach the textArea

Any ideas?

Upvotes: 3

Views: 1145

Answers (2)

Aaron
Aaron

Reputation: 57843

Yes, I had this (nearly) exact same question a few weeks ago, and solved it here: Can't access HyperLinkField text in a GridView.

The way I figured it out, was I debugged my code and checked the values from the immediate window. You'll want to set a breakpoint in the spot in your code where you're iterating through your rows and then check your values (immediate window) like this:

?myGridView.Rows[intRowIndex].Cells[0].Controls[0].Text

When you actually assign it, you might need to cast it in order to get it to work:

par1.Value = ((TextBox)myGridView.Rows[intRowIndex].Cells[0].Controls[0]).Text;

Upvotes: 2

Prince Antony G
Prince Antony G

Reputation: 932

I have a sample gridview, It may help you.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" 
AlternatingRowStyle-BackColor="#006699"  
    AlternatingRowStyle-ForeColor="#FFFFFF" onrowupdating="GridView1_RowUpdating">
<Columns >

<asp:BoundField HeaderText="Name" DataField="uname" />
<asp:BoundField HeaderText="Pass" DataField="upass"/>
<asp:TemplateField>
<HeaderTemplate>Active</HeaderTemplate>
<ItemTemplate >
<asp:TextBox ID="TextArea1" runat="server" TextMode="multiline" Text='<%#Eval("active")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
 <asp:ButtonField CommandName="comment" Text="comment" />
</Columns>
</asp:GridView>

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "comment")
  {
    string uname = "";
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow row = GridView1.Rows[index];
    TextBox txtbox1_val = (TextBox)row.FindControl("TextArea1");
    uname = Server.HtmlDecode(row.Cells[1].Text.ToString());
    //write code here
  }
}

Upvotes: 2

Related Questions