【问题标题】:Can't access the text in the TextArea in a gridview无法访问网格视图中 TextArea 中的文本
【发布时间】:2011-12-29 01:57:09
【问题描述】:
<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);

我无法获取 textArea 中的文本,而 gridview 中的文本。我只是无法从后面的代码中访问它。

我想将textArea 中的文本分配给一个变量,但我无法访问textArea

有什么想法吗?

【问题讨论】:

  • 如果可以,也可以将代码隐藏粘贴到您要分配的位置。
  • 我看不到您的 GridView 或您的代码,以了解您如何迭代其行。

标签: c# asp.net visual-studio visual-studio-2010


【解决方案1】:

我有一个示例网格视图,它可以帮助你。

<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
  }
}

【讨论】:

    【解决方案2】:

    是的,几周前我有这个(几乎)完全相同的问题,并在这里解决了它:Can't access HyperLinkField text in a GridView

    我发现的方法是调试我的代码并检查即时窗口中的值。您需要在代码中迭代行的位置设置断点,然后检查您的值(立即窗口),如下所示:

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

    当你实际分配它时,你可能需要强制转换它才能让它工作:

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

    【讨论】:

    • 是的,当我尝试您的解决方案时,这就是我得到 System.Web.UI.HtmlControls.HtmlTextArea 作为值所以我仍然有问题
    • 我可以看到它到达了 textArea 但仍然不知道如何获取文本,因为我知道获取该文本的唯一方法是 toString,正如我在上面所说的,它只返回 System.Web.UI。 HtmlControls.HtmlTextArea 作为值
    • 用您的代码(您尝试过的)编辑您的帖子,以便我查看。
    猜你喜欢
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    相关资源
    最近更新 更多