通过使用jQuery将值添加到嵌套GridView中的TextBox [英] Add Value to TextBox in Nested GridView by using jQuery

查看:235
本文介绍了通过使用jQuery将值添加到嵌套GridView中的TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的Gridview,它有一个链接用于在嵌套的gridview中显示和隐藏div标签。
默认情况下,这个 Div当我们点击链接显示(启用) Div 标记时,标记被禁用这 Div标签它们是一个 TextBox 和一个按钮

当用户点击一个链接 div将被启用,同时我想添加 @username 在这个文本框中



例如

 < asp:GridView ID =GridParentrunat =server> 
<列>
< asp:TemplateField>
< ItemTemplate>
< table>
< tr>
< td>
-----------
-----------
< / td>
< td>
< asp:GridView ID =GridChildrunat =server>
<列>
< asp:TemplateField>
< ItemTemplate>
< a href ='#'onclick ='showDiv(); return false;'> ShowDiv< / a>
< div id ='divShow'>
< asp:Textbox ID =TextBox1runat =server>< / asp:TextBox>
< asp:Button ID =Button1runat =serverText =Buttononclick = showText();返回false; />
< / div>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>
< / td>
< / tr>
< / table>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>

我正在使用的JQuery

  function showDiv(){
$(#divShow)。show();
$(#TextBox1)。text('@ username');
返回false;
}

上面的jquery代码工作在启用div标签但不添加 @username to TextBox1

解决方案

你的代码有两个问题。首先,ASP.NET为服务器控件生成复杂的ID(类似于 blabla_gridparent_gridchild_textbox1_blabla ,而不是简单的 textbox1 ),所以如果你想和你一起在客户端进行控制,你必须通过 ClientID 您的服务器控件的属性:

  function showDiv(){
$ ( #divShow)显示();
$(#<%= TextBox1.ClientID%>)。val('@ username');
返回false;
}

请注意,这是一个服务器代码。我假设这个JS代码放置在 .aspx 页面(或 .ascx 控件)中,而不是静态的 .js 文件。如果使用静态 .js 文件,则需要将该ID作为参数传递。

其次,您必须使用 .val() 使用jquery方法输入值。 TextBox 被呈现为输入


I have a Nested Gridview having a link is used to show and hide div tag in nested gridview.
By default this Div Tag is disable when we click on a link to show(enable) Div tag under this Div Tag they is one TextBox and one Button.
When user click a link div will be enable at the same time i want to add @username in this textbox

For Example

<asp:GridView ID="GridParent" runat="server">
 <Columns>
   <asp:TemplateField>
     <ItemTemplate>
      <table>
        <tr>
         <td>
           -----------
           -----------
         </td>
         <td>
         <asp:GridView ID="GridChild" runat="server">
          <Columns>
           <asp:TemplateField>
            <ItemTemplate>
               <a href='#' onclick='showDiv(); return false;'>ShowDiv</a>
               <div id='divShow'>
                 <asp:Textbox ID="TextBox1" runat="server"></asp:TextBox>
                 <asp:Button ID="Button1" runat="server" Text="Button" onclick=showText(); return false; />
               </div>
            </ItemTemplate>
           </asp:TemplateField>
         </Columns>
        </asp:GridView>
        </td>
       </tr>
      </table>
     </ItemTemplate>
   </asp:TemplateField>
 </Columns>
</asp:GridView>

JQuery which i'm using

function showDiv() {            
         $("#divShow").show();
         $("#TextBox1").text('@username');
         return false;
        }

the above jquery code working to Enable Div Tag but not adding @username to TextBox1

解决方案

You have two issues with your code.

First, ASP.NET generates complex IDs for server controls (something like blabla_gridparent_gridchild_textbox1_blabla, instead of simple textbox1), so if you want to work with you control on client side, you have to ask ASP.NET for that ID via ClientID property of your server control:

function showDiv() {            
     $("#divShow").show();
     $("#<%= TextBox1.ClientID %>").val('@username');
     return false;
}

Note, this is a server code. I assume this JS code placed into your .aspx page (or .ascx control), not in static .js file. In case of static .js file you need to pass that ID as parameter.

Second, you have to use .val() jquery method to work with input value. TextBox is rendered into input.

这篇关于通过使用jQuery将值添加到嵌套GridView中的TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆