使用JavaScript在gridview控件中的文本框 [英] textbox in gridview control using javascript

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

问题描述



我在gridview行中插入数据.我分别有2个文本框,1个标签和1个文本框.在这里,我要对前两个文本框中的数据进行计算,结果应该显示在标签中,标签中的文本要乘以40,然后应该显示在最后一个文本框中.

该操作在第二个文本框的onblur()事件中完成.为此,我需要在textbox的onblur()事件中的gridview中找到控件.

我尝试在.cs文件中执行此操作,但是此操作必须在插入到gridview特定行上的数据库之前完成.

我在网上搜索,但找不到解决方案.我不知道如何使用javascript
查找gridview的文本框
请帮助我.....

Hi,

I am inserting a data in gridview row. I have 2 textbox''s, 1 label and 1 textbox respectively. Here, I want to do calculations on data which is in first two textboxes and result should be displayed in label and the text in label to be multiplied with 40 and that should be displayed in last textbox.

This operation to be done in onblur() event of second textbox. To do this I need to find the controls in gridview in onblur() event of the textbox.

I tried to do this in .cs file but this operation is to be done before inserting in to database on that particular row of gridview.

I searched on the net but i couldn''t find the solution. I couldn''t know how to find textbox of a gridview using javascript

Please help me.....

推荐答案

您可以在此处获得解决方案

http://forums.asp.net/t/1194696.aspx [
you can get solution here

http://forums.asp.net/t/1194696.aspx[^]


<big>Client side code</big>

<big>Client side code</big>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    function DoCalculation(Txt1,Txt2,Txt3,Lbl1)
    {
      Txt1=document.getElementById(Txt1);
      Txt2=document.getElementById(Txt2);
      Txt3=document.getElementById(Txt3);
      Lbl1=document.getElementById(Lbl1);
      Lbl1.innerHTML=parseFloat(Txt1.value)+parseFloat(Txt2.value);
      Txt3.value=parseFloat(Lbl1.innerHTML)/40;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("C1") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("C2") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("C3") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("C4") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:ButtonField CommandName="Update" Text="Update" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>



<big>Server Side Code</big>



<big>Server Side Code</big>

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            DoBindDB();
    }

    public void DoBindDB()
    {
        DataSet _Ds = new DataSet();
        _Ds.Tables.Add();
        _Ds.Tables[0].Columns.Add("C1");
        _Ds.Tables[0].Columns.Add("C2");
        _Ds.Tables[0].Columns.Add("C3");
        _Ds.Tables[0].Columns.Add("C4");

        for (int i = 0; i < 10; i++)
        {
            DataRow Dr = _Ds.Tables[0].NewRow();
            Dr["C1"] = 0;
            Dr["C2"] = 0;
            Dr["C3"] = 0;
            Dr["C4"] = 0;
            _Ds.Tables[0].Rows.Add(Dr);
        }

        GridView1.DataSource = _Ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName = "Update")
        //{
        //}
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox Txt1 = (TextBox)e.Row.Cells[0].Controls[1];
            TextBox Txt2 = (TextBox)e.Row.Cells[1].Controls[1];
            Label Lbl1 = (Label)e.Row.Cells[2].Controls[1];
            TextBox Txt3 = (TextBox)e.Row.Cells[3].Controls[1];
            Txt1.Attributes["onkeyup"] = "DoCalculation(''" + Txt1.ClientID + "'',''" + Txt2.ClientID + "'',''" + Txt3.ClientID + "'',''" + Lbl1.ClientID + "'');";
            Txt2.Attributes["onkeyup"] = "DoCalculation(''" + Txt1.ClientID + "'',''" + Txt2.ClientID + "'',''" + Txt3.ClientID + "'',''" + Lbl1.ClientID + "'');";
        }
    }


如果您希望使用客户端脚本访问字段(我认为是这种情况),则可以在浏览器中查看网页的来源.在源代码中查找文本框,然后找到每个文本框的id属性.然后,您可以使用id属性中包含的值来引用客户端脚本中的文本框:

if you''re looking to access the fields using client-side script, which I think is the case, you can view the source of the webpage in the browser. look for the textboxes in the source and find the id attribute of each textbox. you can then reference the textbox in client-side script using the value contained in the id attribute:

var textbox = document.getElementById("ID OF TEXTBOX GOES HERE");

//other code..



尽管这是完全有效的并且可以在几乎所有浏览器中使用,但是当我希望与网页的HTML进行交互时,我总是喜欢使用诸如jquery之类的javascript框架.如果您在页面中引用了jquery,那么您将编写如下内容:



while this is perfectly valid and will work in just about any browser, I''d always favour using a javascript framework such as jquery when I''m looking to interact with the HTML of a web page. provided you have jquery referenced in the page you would then write something like this:

var textbox =


这篇关于使用JavaScript在gridview控件中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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