如何避免在asp.net按钮单击事件后刷新页面 [英] How to avoid page refresh after button click event in asp.net

查看:184
本文介绍了如何避免在asp.net按钮单击事件后刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是下面的code中的页面只要点击btninsert事件完成后我想停止页面刷新后btninsert是点击刷新,

 < ASP:的UpdatePanel ID =UpdatePanel1=服务器>    <&的ContentTemplate GT;
        < D​​IV的风格=保证金底:20像素;的margin-top:20px的;><跨度><强作风=FONT-SIZE:大;>编辑用户< / STRONG>< / SPAN&GT ;< / DIV>
        < D​​IV>
            <跨度风格=浮动:左;宽度:50%;>&安培; NBSP;< / SPAN> <跨度风格=浮动:左;宽度:50%;>
                <跨度风格=宽度:100%;浮动:左;文本对齐:权利;>
                    < ASP:标签ID =lblMessage=服务器文本= - >< / ASP:标签>< / SPAN>
            < / SPAN>
        < / DIV>        < D​​IV的风格=宽度:100%;浮动:左;>
            <小时/>
        < / DIV>
        < D​​IV的风格=WIDTH:816px;保证金左:为5px;的margin-top:20px的;高度:463px;>            < D​​IV的风格=宽度:100%;浮动:左;填充顶:15px的;高度:257px;保证金左:0像素;>
                <跨度类=鸿沟>
                    <跨度类=simDivide1>全名< / SPAN>
                    <跨度类=simDivide>
                        < ASP:文本框ID =txtfullname=服务器WIDTH =180像素>
                        < / ASP:文本框>
                    < / SPAN>
                < / SPAN>
                <跨度类=鸿沟>
                    <跨度类=simDivide1>< / SPAN>
                    <跨度类=simDivide>< / SPAN>
                < / SPAN>
                <跨度类=鸿沟>
                    <跨度类=simDivide1>用户名和LT; / SPAN>
                    <跨度类=simDivide>
                        < ASP:文本框ID =txtusername=服务器WIDTH =180像素>
                        < / ASP:文本框>
                    < / SPAN>
                < / SPAN>                <跨度类=鸿沟>
                    <跨度类=simDivide1>< / SPAN>
                    <跨度类=simDivide>< / SPAN>
                < / SPAN>                <跨度类=鸿沟>
                    <跨度类=simDivide1>密码和LT; / SPAN>
                    <跨度类=simDivide>
                        < ASP:文本框ID =txtpassword=服务器WIDTH =180像素>
                        < / ASP:文本框>
                    < / SPAN>
                < / SPAN>                <跨度类=鸿沟>
                    <跨度类=simDivide1>< / SPAN>
                    <跨度类=simDivide>< / SPAN>
                < / SPAN>
                <跨度类=鸿沟>
                    <跨度类=simDivide1>手机号码
                    < / SPAN>
                    &安培; NBSP;<跨度类=simDivide>< ASP:文本框ID =txtmobileno=服务器WIDTH =180像素>
                    < / ASP:文本框>
                    < / SPAN>
                < / SPAN>                <跨度类=鸿沟>                    <跨度类=simDivide>< / SPAN>
                < / SPAN>
                <跨度类=鸿沟>
                    <跨度类=simDivide1>作用
                    < / SPAN>
                    &安培; NBSP;<跨度类=simDivide>< ASP:文本框ID =txtrole=服务器WIDTH =180像素>
                    < / ASP:文本框>
                    < / SPAN>
                < / SPAN>                <脚本SRC =jQuery的-2.0.2.js>< / SCRIPT>                < SCRIPT LANGUAGE =JavaScript的>                    函数来完成(){
                        VAR列表=的document.getElementById(TID);
                        list.removeChild(list.lastChild);
                    }
                    功能changecourse(五){                        VAR变化=的document.getElementById('MYTEXT')值。
                        变种I = 1;                        mNewObj =使用document.createElement('DIV');
                        mNewObj.id =BOX+我;
                        mNewObj.style.visibility =秀;
                        mNewObj.innerHTML =变化+&放大器; NBSP< A HREF =#风格='文字装饰:无;颜色:红色的onClick ='()完成'> X< / A>中;
                        的document.getElementById(TID)的appendChild(mNewObj)。
                        我++
                        VAR一个=的document.getElementById('MYTEXT')的selectedIndex。
                        。的document.getElementById(ContentPlaceHolder1_Hidden1)值=一个;
                        //document.getElementById(\"ContentPlaceHolder1_btninsert),点击()。                        删除();
                    }                    功能有(){                        $(#ContentPlaceHolder1_btninsert),点击()。                    }
                    //函数刀片(){                    // $阿贾克斯({
                    //类型:POST,
                    // URL:Edituser.aspx.cs /插入                    //成功:函数(){警报(成功); },
                    //错误:函数(){警报('错误'); }                    //});
                    //}
                    功能取消(){                        VAR选择=的document.getElementById('MYTEXT');
                        select.remove(select.selectedIndex);
                    }
                    函数删除(){                        。的document.getElementById(MYTEXT)的style.display ='无';
                        的document.getElementById(Button1的)的style.display ='无'。
                        。的document.getElementById(TID)的style.display =内联;
                        。的document.getElementById(myLink的)的style.display =内联;
                    }                    功能showdiv(){                        。的document.getElementById(myLink的)的style.display ='无';
                        的document.getElementById(MYTEXT)的style.display =内联。
                        。的document.getElementById(Button1的)的style.display =内联;
                    }
                < / SCRIPT>            <输入ID =Hidden1类型=隐藏=服务器/>
            < / DIV>
            < ASP:按钮的ID =btnUpdate=服务器的OnClick =btnUpdate_Click风格=保证金左:5像素文本=编辑WIDTH =39px/>
            < BR>
            < BR>            < ASP:的UpdatePanel =服务器>
                <&的ContentTemplate GT;
           &安培;&NBSP放大器;&NBSP放大器;&NBSP放大器;&NBSP LT; D​​IV ID =TID>
                               < / DIV>
            < D​​IV ID =嘀>                <一个ID =myLink的的onclick =showdiv()>添加站及LT; / A>                <选择一个id ='MYTEXT的名字='MYTEXT风格=显示:无>
                    <选项> - 选择 - < /选项>
                    <选项>孟买与LT; /选项>
                    <选项>&德里LT; /选项>
                    <选项>&班加罗尔LT; /选项>
                    <选项>&艾哈迈达巴德LT; /选项>
                < /选择>
               <输入类型=按钮ID =Button1的风格=显示:无的onclick =changecourse();是();取消();返回false;值=添加/>
                < / DIV>
            < ASP:按钮的ID =btninsert=服务器风格=显示:无的OnClick =btninsert_Click文本=插入的ValidationGroup =C/>                < /&的ContentTemplate GT;
                < / ASP:的UpdatePanel>
             < /&的ContentTemplate GT;
< / ASP:的UpdatePanel>

这是Edit.aspx.cs中,我有方法

 保护无效btninsert_Click(对象发件人,EventArgs的发送)
    {
        字符串= Hidden1.Value;
        字符串userid = Convert.ToString(会话[登录ID]);
        con.Open();
        CMD的SqlCommand =新的SqlCommand(INSERT INTO UserDepot(用户ID,DepotId)+
            VALUES('+用户ID +,+ A +'),CON);
        cmd.ExecuteNonQuery();
        con.Close();
    }


解决方案

添加的OnClientClick =返回false;

 < ASP:按钮的ID =btninsert=服务器文本=按钮的OnClientClick =返回false; />

或codeBehind:

  btninsert.Attributes.Add(的onclick,返回false;);

This is the following code the page refreshes as soon as the btninsert click event is completed i want to stop the page refresh after the btninsert is click

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>


        <div style="margin-bottom: 20px; margin-top: 20px;"><span><strong style="font-size: large;">Edit User</strong></span></div>
        <div>
            <span style="float: left; width: 50%;">&nbsp;</span> <span style="float: left; width: 50%;">
                <span style="width: 100%; float: left; text-align: right;">
                    <asp:Label ID="lblMessage" runat="server" Text="-"></asp:Label></span>
            </span>
        </div>

        <div style="width: 100%; float: left;">
            <hr />
        </div>


        <div style="width: 816px; margin-left: 5px; margin-top: 20px; height: 463px;">

            <div style="width: 100%; float: left; padding-top: 15px; height: 257px; margin-left: 0px;">
                <span class="Divide">
                    <span class="simDivide1">FullName</span>
                    <span class="simDivide">
                        <asp:TextBox ID="txtfullname" runat="server" Width="180px">
                        </asp:TextBox>
                    </span>


                </span>
                <span class="Divide">
                    <span class="simDivide1"></span>
                    <span class="simDivide"></span>
                </span>


                <span class="Divide">
                    <span class="simDivide1">Username</span>
                    <span class="simDivide">
                        <asp:TextBox ID="txtusername" runat="server" Width="180px">
                        </asp:TextBox>
                    </span>
                </span>

                <span class="Divide">
                    <span class="simDivide1"></span>
                    <span class="simDivide"></span>
                </span>

                <span class="Divide">
                    <span class="simDivide1">Password</span>
                    <span class="simDivide">
                        <asp:TextBox ID="txtpassword" runat="server" Width="180px">
                        </asp:TextBox>
                    </span>
                </span>

                <span class="Divide">
                    <span class="simDivide1"></span>
                    <span class="simDivide"></span>
                </span>


                <span class="Divide">
                    <span class="simDivide1">Mobile No
                    </span>
                    &nbsp;<span class="simDivide"><asp:TextBox ID="txtmobileno" runat="server" Width="180px">
                    </asp:TextBox>
                    </span>
                </span>

                <span class="Divide">

                    <span class="simDivide"></span>
                </span>


                <span class="Divide">
                    <span class="simDivide1">Role
                    </span>
                    &nbsp;<span class="simDivide"><asp:TextBox ID="txtrole" runat="server" Width="180px">
                    </asp:TextBox>
                    </span>
                </span>



                <script src="jquery-2.0.2.js"></script>

                <script language="javascript">

                    function done() {
                        var list = document.getElementById("tid");
                        list.removeChild(list.lastChild);


                    }


                    function changecourse(e) {

                        var change = document.getElementById('mytext').value;
                        var i = 1;

                        mNewObj = document.createElement('div');
                        mNewObj.id = "BOX" + i;
                        mNewObj.style.visibility = "show";
                        mNewObj.innerHTML = change + "&nbsp<a href='#' style='text-decoration: none; color:red' onClick='done()'> x </a> ";


                        document.getElementById("tid").appendChild(mNewObj);
                        i++
                        var a = document.getElementById('mytext').selectedIndex;
                        document.getElementById("ContentPlaceHolder1_Hidden1").value = a;
                        //document.getElementById("ContentPlaceHolder1_btninsert").click();

                        deleted();




                    }

                    function yes() {

                        $("#ContentPlaceHolder1_btninsert").click();

                    }


                    //function insert() {

                    //    $.ajax({
                    //        type: "POST",
                    //        url: "Edituser.aspx.cs/insert",

                    //        success: function () { alert('success'); },
                    //        error: function () { alert('error'); }

                    //    });


                    //}


                    function cancel() {

                        var select = document.getElementById('mytext');
                        select.remove(select.selectedIndex);
                    }


                    function deleted() {

                        document.getElementById("mytext").style.display = 'none';
                        document.getElementById("Button1").style.display = 'none';
                        document.getElementById("tid").style.display = 'inline';
                        document.getElementById("mylink").style.display = 'inline';
                    }



                    function showdiv() {

                        document.getElementById("mylink").style.display = 'none';
                        document.getElementById("mytext").style.display = 'inline';
                        document.getElementById("Button1").style.display = 'inline';


                    }


                </script>



            <input id="Hidden1" type="hidden" runat="server" />
            </div>
            <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Style="margin-left: 5px" Text="Edit" Width="39px" />
            <br>
            <br>

            <asp:UpdatePanel runat="server">
                <ContentTemplate>
           &nbsp&nbsp&nbsp&nbsp<div id="tid" >
                               </div>
            <div id="di">

                <a id="mylink" onclick="showdiv()">Add Depot</a>

                <select id='mytext' name='mytext' style="display: none">
                    <option>--Select--</option>
                    <option>Mumbai</option>
                    <option>Delhi</option>
                    <option>Banglore</option>
                    <option>Ahmedabad</option>
                </select>
               <input type="button" id="Button1" style="display: none" onclick=" changecourse(); yes(); cancel(); return false;" value="add" />
                </div>
            <asp:Button ID="btninsert" runat="server"  Style="display: none" OnClick="btninsert_Click" Text="Insert" ValidationGroup="C" />

                </ContentTemplate>
                </asp:UpdatePanel>
             </ContentTemplate>
</asp:UpdatePanel>

and this is Edit.aspx.cs in which i have method

    protected void btninsert_Click(object sender, EventArgs e)
    {
        string a = Hidden1.Value;
        string UserId = Convert.ToString(Session["LoginId"]);
        con.Open();
        SqlCommand cmd = new SqlCommand("INSERT INTO UserDepot (UserId,DepotId)" +
            "VALUES ('" + UserId + "','" + a + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();


    }

解决方案

Add OnClientClick="return false;" ,

<asp:button ID="btninsert" runat="server" text="Button" OnClientClick="return false;" />

or in the CodeBehind:

 btninsert.Attributes.Add("onclick", "return false;");

这篇关于如何避免在asp.net按钮单击事件后刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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