使用JQuery BlockUI访问GridView内的TextArea

时间:2009-05-19 19:00:17

标签: asp.net

这让我疯了!

我正在尝试访问GridView控件中的TextArea。单击gridview上的按钮时会弹出TextArea。由于某种原因,textarea.value总是包含“”。

<asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="false" 
            onrowcommand="gvCategories_RowCommand">

    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
        <input type="button" value="add comment" onclick="showCommentBox()" />
    </ItemTemplate>
    </asp:TemplateField>

     <asp:TemplateField>
    <ItemTemplate>
       <div id="commentBox" style="display:none">

     <input type="button" value="move comment input box" onclick="moveComment()" /> 


    <textarea id="txtComment" rows="10" cols="30">
    </textarea>

    </div>   
    </ItemTemplate>
    </asp:TemplateField>

    </Columns>

    </asp:GridView>




function moveComment() {

        alert(document.getElementById("txtComment").value);  

    }

我在服务器端添加了此代码,但TextBox始终返回“”

  protected void gvCategories_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            var row = (GridViewRow) (e.CommandSource as LinkButton).NamingContainer;
            var description = (row.FindControl("txtDescription") as TextBox).Text;
            lblComment.Text = description; 
        }

2 个答案:

答案 0 :(得分:2)

@Azam - 这与我回答的其他post有关。 gridview使用相同的ID集多次生成commentBox DIV及其所有子元素。

我对此进行了测试,发现每次调用document.getElementById("txtComment")都会返回DOM中的下一个匹配元素,直到它循环遍历整个匹配元素集合,返回到第一个匹配元素然后它重新做到了。

这就是为什么在尝试访问textarea或文本框的值时会出现空白的原因。

您需要修改对showComment()的调用,以便它存储对给定行中元素的引用,然后当您调用moveComment()时,它将在同一元素上工作,而不仅仅是DOM中具有相同ID的下一个元素。

答案 1 :(得分:0)

尝试textarea.innerHTML

查看您的代码:

alert(document.getElementById("txtComment").innerHTML);