如何在asp.net中添加gridview的列的值 [英] How add values of columns of gridview in asp.net

查看:74
本文介绍了如何在asp.net中添加gridview的列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net中添加gridview的列的值

How add values of columns of gridview in asp.net

推荐答案

示例代码为:

Sample code is given as:

<script language ="javascript" type ="text/javascript" >
    function setValue(num)
    {
        var t1 = document.getElementById("GridView1_ctl0" + num + "_TextBox1");
        var t2 = document.getElementById("GridView1_ctl0" + num + "_TextBox2");
        var t3 = document.getElementById("GridView1_ctl0" + num + "_TextBox3");
        var t4 = document.getElementById("GridView1_ctl0" + num + "_TextBox4");
        if (isInteger(t1.value) && isInteger(t2.value) && isInteger(t3.value))
            t4.innerText = parseFloat(t1.value) + parseFloat(t2.value) + parseFloat(t3.value);
    }

    function isInteger(s)
    {
        var i;
     s = s.toString();
        for (i = 0; i < s.length; i++)

        {

            var c = s.charAt(i);

            if (isNaN(c))

         {

          alert("Given value is not a number");

          return false;

         }

        }

        return true;

    }

</script>

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" >
    <Columns>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label runat="server" ID="Label1" Text='<%# Eval("Name") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox runat="server" ID="TextBox1" ></asp:TextBox>
    <asp:TextBox runat="server" ID="TextBox2" ></asp:TextBox>
    <asp:TextBox runat="server" ID="TextBox3" ></asp:TextBox>
    <asp:TextBox runat="server" ID="TextBox4"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%


ConnectionStrings:sql %> " span> SelectCommand =" 从[表格]中选择*" < /asp:SqlDataSource > 受保护的void GridView1_RowDataBound(对象发送者,GridViewRowEventArgs e) { 如果(e.Row.RowType == DataControlRowType.DataRow) { TextBox tb1 =(TextBox)e.Row.FindControl("TextBox1"); TextBox tb2 =(TextBox)e.Row.FindControl("TextBox2"); TextBox tb3 =(TextBox)e.Row.FindControl("TextBox3"); tb1.Attributes.Add("onchange","setValue(" +(e.Row.TabIndex + 2).ToString()+)"); tb2.Attributes.Add("onchange","setValue(" +(e.Row.TabIndex + 2).ToString()+)"); tb3.Attributes.Add("onchange","setValue(" +(e.Row.TabIndex + 2).ToString()+)"); } }
ConnectionStrings:sql %>" SelectCommand="select * from [table]"> </asp:SqlDataSource> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { TextBox tb1 = (TextBox)e.Row.FindControl("TextBox1"); TextBox tb2 = (TextBox)e.Row.FindControl("TextBox2"); TextBox tb3 = (TextBox)e.Row.FindControl("TextBox3"); tb1.Attributes.Add("onchange", "setValue(" + (e.Row.TabIndex + 2).ToString() + ")"); tb2.Attributes.Add("onchange", "setValue(" + (e.Row.TabIndex + 2).ToString() + ")"); tb3.Attributes.Add("onchange", "setValue(" + (e.Row.TabIndex + 2).ToString() + ")"); } }



foreach (GridViewRow row in GridView1.Rows)
{
   string storeData= row.Cells[2].Text;
}


这篇关于如何在asp.net中添加gridview的列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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