当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to copy data from textbox to another textbox, added dynamically in gridview, without postback

问题描述:

I've dynamically added n number of textbox in gridview and bind the data on them. I want a code behind function/method to sum textbox1, textbox2.....textboxn in textboxtotal, if user change the value in any text box. I can't add postback with these textboxes, because after postback textbox get disposed.

Code for adding Columns to Gridview dynamically

 foreach (string a in crcl)

{

SqlCommand cmd1 = new SqlCommand("select qty from purchaseinstruction where project ='" + DropDownList1.SelectedItem.ToString() + "' AND circle = '" + a + "' AND item = 'BIFURCATION' AND material = '" + mat + "'", agr);

SqlDataReader dr1 = cmd1.ExecuteReader();

if (dr1.Read())

{

string val = dr1[0].ToString();

if (val.Length > 0)

{

row[a] = val;

value = value + decimal.Parse(val);

}

else

{

row[a] = 0;

}

}

else

{

row[a] = 0;

}

dr1.Dispose();

row["TOTAL"] = value;

}

dt.Rows.Add(row);

GridView1.DataSource = dt;

GridView1.DataBind();

Adding Textbox control on rowbound:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

int i = 3;

if (e.Row.RowType == DataControlRowType.DataRow)

{

crcl = (List<string>)ViewState["bdi2"];

foreach(string a in crcl)

{

TextBox TextBox101 = new TextBox();

TextBox101.ID=a;

TextBox101.Width = 60;

TextBox101.Text = (e.Row.DataItem as DataRowView).Row[a].ToString();

e.Row.Cells[i].Controls.Add(TextBox101);

i++;

}

TextBox TextBox102 = new TextBox();

TextBox102.ID = "TOTAL";

TextBox102.Width = 60;

TextBox102.Text = (e.Row.DataItem as DataRowView).Row["TOTAL"].ToString();

e.Row.Cells[i].Controls.Add(TextBox102);

}

}

网友答案:

Here is a little example to get you started. Add a unique class name to the TextBoxes in the GridView. Then the jQuery can bind the onblur event to them.

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" CssClass="GridTextBox"></asp:TextBox>
                <asp:TextBox ID="TextBox2" runat="server" CssClass="GridTextBox"></asp:TextBox>
                <asp:TextBox ID="TextBox3" runat="server" CssClass="GridTextBox"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And here the javascript function that is called when the TextBox loses focus. First the ID of the TextBox that triggers the event is split into pieces to get the ID's of the other two TextBoxes in the row ( in my case the ID looks like this ContentPlaceHolder1_GridView1_TextBox1_0). Then it's just a matter of adding the values.

<script type="text/javascript">
    $(document).ready(function () {
        $('.GridTextBox').blur(function () {
            var idString = this.id.split("_");
            var tb1 = idString[0] + "_" + idString[1] + "_TextBox1_" + idString[3];
            var tb2 = idString[0] + "_" + idString[1] + "_TextBox2_" + idString[3];
            var tb3 = idString[0] + "_" + idString[1] + "_TextBox3_" + idString[3];
            var total = parseInt($("#" + tb1).val()) + parseInt($("#" + tb2).val());
            if (!isNaN(total)) {
                $("#" + tb3).val(total);
            }
        });
    });
</script>
网友答案:

You can create Javascript function in code behind, like this (if you are using WebForms):

string myScript = "function sumTextBoxes() { var arr = document.getElementsByClassName('txtBox');\n";
       myScript += "var total=0;\n";
       myScript += "for(var i=0;i<arr.length;i++){\n";
       myScript += "if(parseInt(arr[i].value))\n";
       myScript += "total += parseInt(arr[i].value);}\n";
       myScript += "document.getElementById('total').value = total;}\n";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, true);

Now, add css class to your dynamicaly created TextBoxes (add this):

TextBox101.CssClass = "txtBox";

Also, TextBox which displays the sum should be named like this:

TextBox102.ID = "total";
分享给朋友:
您可能感兴趣的文章:
随机阅读: