开发者

How to write and execute asp.net textchange event handler in javascript and code behind both?

开发者 https://www.devze.com 2023-01-31 13:16 出处:网络
net website, i have textbox control inside datagrid control. I would like to add textchange event in javascript where i need to sum the values inside textboxes in datagrid and show that addition in l

net website, i have textbox control inside datagrid control.

I would like to add textchange event in javascript where i need to sum the values inside textboxes in datagrid and show that addition in lable outside grid.

I would also like to do same addition in codebehind(*.cs)

But codebehind only execute when browser not support javascript. It means when browser 开发者_StackOverflowsupport javascript only client side javascript should execute not server side code


Are you using the textboxes as EditItemTemplate? If so, within the textboxes in the EditItemTemplate add the OnTextChanged event - Textbox1_OnTextChanged as below.

<asp:DataGrid ID="Grid" runat="server">
        <Columns>
            <asp:TemplateColumn>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" OnLoad="TextBox1_Load" OnTextChanged="Textbox1_OnTextChanged"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>

You can then sumup the values in the serverside.

Also, you have to add the Load event(Textbox1_OnLoad) in the EditItemTemplate for the textbox to bind the clientside event of the text box as below.

 protected void TextBox1_Load(object sender, EventArgs e)
    {
        TextBox newTb = sender as TextBox;
        if (newTb != null)
        {
            newTb.Attributes.Add("onChange", "sumup(this)");
        }
    }

In Javascript, you can handle the sumup event and get the value of the textboxes using the this object and add the values. If the javascript is supported the sum will be calculated and you should return false at the end of the sumup function to stop the postback.

If the javascript is not supported, then the method will not be called and the postback will be automatically triggered.

Hope this should give you better idea.

Regards,

Lakxman Kumar C

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号