使用javascript从gridview中存在的标签或文本框中检索文本或值 [英] retriving text or values from lables or textbox present in gridview using javascript

查看:53
本文介绍了使用javascript从gridview中存在的标签或文本框中检索文本或值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格,如下所示

I have a grid as follow

<asp:gridview id="GridView1" runat="server" style="z-index: 105; left: 413px; position: absolute;top: 261px" autogeneratecolumns="False" backcolor="#CCCCCC" bordercolor="#999999" borderstyle="Solid" borderwidth="3px" cellpadding="4" cellspacing="2" forecolor="Black" xmlns:asp="#unknown">
<columns>
   <asp:templatefield showheader="False">
   <itemtemplate>
     <asp:linkbutton id="LinkButton1" runat="server" onclientclick="return confirmdelete();" causesvalidation="False" commandname="Delete" text="Delete"></asp:linkbutton>
   </itemtemplate>
   </asp:templatefield>
   <asp:templatefield headertext="dob" sortexpression="dob">
   <edititemtemplate>
     <asp:textbox id="TextBox1" runat="server" text="<%# Bind(" dob=") %>"></asp:textbox>
   </edititemtemplate>
   <itemtemplate>
     <asp:label id="Label1" runat="server" text="<%# Bind(" dob=") %>"></asp:label>
   </itemtemplate>
   </asp:templatefield>
   <asp:templatefield headertext="age" sortexpression="age">
   <edititemtemplate>
     <asp:textbox id="TextBox2" runat="server" text="<%# Bind(" age=") %>"></asp:textbox>
     </edititemtemplate>
   <itemtemplate>
     <asp:label id="Label2" runat="server" text="<%# Bind(" age=") %>"></asp:label>
   </itemtemplate>
   </asp:templatefield>
</columns>
<footerstyle backcolor="#CCCCCC" />
<rowstyle backcolor="White" />
<selectedrowstyle backcolor="#000099" font-bold="True" forecolor="White" />
<pagerstyle backcolor="#CCCCCC" forecolor="Black" horizontalalign="Left" />
<headerstyle backcolor="Black" font-bold="True" forecolor="White" />
</asp:gridview>




我希望当用户单击gridview年龄中存在的链接按钮时,标签中的当前按钮应该使用javascript检索.我尝试使用隐藏字段.并且gridview rowdatabound我成功了,但是现在我不想使用隐藏字段.那么我尝试将代码作为




I want that when a user click the linkbutton present in gridview age which is presnt in label should b retrive using javascript.i have tried using a hidden field .and gridview rowdatabound i was success.but now i dont want to use hidden field.then i tryied code as

function confirmdelete()
{
   var grid=document.getElementById(''<%=GridView1.ClientID%>'');
  //{document.g
  var age=grid.getElementsByName("Label2");
  document.write(age.value);
}


但我无法这样做.
在gridview.plz中也有很多行.


but i m not able to do so.
also there are many row present in gridview.plz help

推荐答案

您可以通过以下方式获取它

1.将javascript"onclick"事件方法附加到GridView的RowDataBound事件中的每个LinkBut​​ton,如下所示(在CodeBehind文件中):

You can get it in the following way

1. Attach a javascript "onclick" event method to each LinkButton in the GridView''s RowDataBound event as follows (In the CodeBehind file):

//Binding some sample data to the GridView
 protected void Page_Load(object sender, EventArgs e)
 {
        IList items = new ArrayList();
        items.Add("Shubho");
        items.Add("John");
        items.Add("Tina");

        GridView1.DataSource = items;
        GridView1.DataBind();
 }
  //Bind the confirmDelete() onclick event method to each LinkButton inside GridView''s row
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl = e.Row.FindControl("Label1") as Label;
            LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
            if (lbl != null &amp;&amp; link != null)
            {
                lbl.Text = (string)e.Row.DataItem;
                link.Attributes["onclick"] = "return confirmDelete(''" + lbl.ClientID + "'')";
            }
        }
 } 



请注意,confirmDelete()javascript方法与RowDataBound()事件方法中的客户端ID(Asp.net Label控件的JavaScript ID)一起传递.
2.由于CodeBehind中附带了"onclick" javascript方法,因此无需在GridView的LinkBut​​ton上使用"onclientclick".因此,删除"onclientclick"属性并按如下所示定义javascript方法,该方法接受同一行中Label的ID,并检索该值以提示用户:



Note that, the confirmDelete() javascript method is passed with the client ID (JavaScript ID of the Asp.net Label control) in the RowDataBound() event method.

2. As the "onclick" javascript method is attached in the CodeBehind, there is no need to use "onclientclick" on the LinkButton in the GridView. So, remove the "onclientclick" attribute and define the confirmDelete() javascript method as follows, that accepts the id of the Label in the same row, and retrieves the value to prompt the user:

<div>
        <script language="javascript">
            function confirmDelete(label) {
                var input = document.getElementById(label).innerHTML;
                return confirm("Are you sure to delete '" + input + "'?");
            }
        </script>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"

            Width="215px">
            <Columns>
                <asp:TemplateField HeaderText="Data">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"

                            Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>



上面的代码是有效的,您可以使用它来运行和测试,以便您可以清楚地了解正在发生的事情.使用上面的方法,您可以读取GridView行中的任何控件的值使用javascript.随时使用ConfirmDelete()方法中的javascript做任何事情.

希望对您有所帮助:)



The above codes are functional and you can use it to run and test so that you can have a clear understanding of what''s happening.Using the above approach, you can read any control''s value inside the GridView''s rows using the javascript. Feel free to do any thing using the javascript in the confirmDelete() method.

Hope it helps :)


这篇关于使用javascript从gridview中存在的标签或文本框中检索文本或值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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