领悟旧事

Learned Helplessness
分类新闻>>技术文章>>
asp.net中如何利用UpdataPanel来提交数据?--错误!
来源:http://www.xx0594.com/
时间:2017-5-2
作者:  浏览人数:940
[Size=3]在做项目时,遇到一个问题,在提交一个数据时,先要判断名字、题目、验证码等的正确性,如果不正确,就弹出对话框,如果正确,就提交数据。
    第一步,还算简单,只要利用UpdataPanel就可以了,代码如下:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True">
        <ContentTemplate>
            <asp:Button ID="Submit_Btn" runat="server" Font-Size="18px" Text="提交" CssClass="YellowBtn" OnClick="Submit_Btn_Click" />
            <asp:Button ID="Reset_top" runat="server" Font-Size="18px" Text="重置" CssClass="PinkBtn" />
        </ContentTemplate>
    </asp:UpdatePanel>

   C#的后台代码如下:
[color=#0000FF]
protected void Submit_Btn_Click(object sender, EventArgs e)
        {
            if (Title_TextBox.Text == "")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "click", "alert('标题为空!')", true);
                return;
            }
            if (UserName_TextBox.Text == "")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "click", "alert('作者为空!')", true);
                return;
            }
            String sChcode = Session["CheckCode"].ToString();
            if (String.Equals(CheckCode_TextBox.Text, sChcode) == false)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "click", "alert('验证码不对!');", true);
                return;
            }
            String Sc = PingLunTextBox.InnerText;            
            if (Sc == null || Sc=="")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "click", "alert('内容不能为空!')", true);
                return;
            }
            //设置SQL语句
            String sRec = Request["LMQ"] + "," + HttpContext.Current.Request.UserHostAddress.ToString() + "," + DateTime.Now.ToString() + "," + Sc;
            if (ZiDuan_ListBox.SelectedIndex == 0) sRec += ",zhushi";//写的是注释。
            else sRec += ",shangXi";//写的是注释。
            XianShiWen.MyClass.InSertRec("userInfo.mdb", "ArticlePingLun", "ArticleID, UserIP, WriteDate, PingLun, PingLunZiDuan", sRec);//插入记录。
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "click", "alert('谢谢您的支持!');location='default.asp';", true);
        }
[/color]
    样第一个目的是达到了,但却出现了就一奇异现象。也就是在提交时,第一次点击按钮时会出现“内容为空”的提示,只要在第二次点击提交时,才能正确得到结果。
    经过日日夜夜的折腾和煎熬,最后发现,只要去掉UpdataPanel就可以正确得到结果了。那怎么办?
    既要不刷新页面正确地判断人名、题目、验证码的正确与否,又要一次性的正确的提交数据。怎么办,头晕且大!
    又经过无数个日日夜夜的折腾和煎熬,最后终于熬到了尽头。原来,真相就隔着一层纸哪!
    其实只要简单添加一句就解决了头疼且大的问题。总的代码如下:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True">
        <ContentTemplate>
            <asp:Button ID="Submit_Btn" runat="server" Font-Size="18px" Text="提交" CssClass="YellowBtn" OnClick="Submit_Btn_Click" />
            <asp:Button ID="Reset_top" runat="server" Font-Size="18px" Text="重置" CssClass="PinkBtn" />
        </ContentTemplate>

        <Triggers>
            <asp:PostBackTrigger ControlID="Submit_Btn" />
        </Triggers>

    </asp:UpdatePanel>

[/Size]

 
 
 
 
 
 
 
 
 

关闭窗口
 访问量:507988
任何第三方若要引用、转摘本站信息,均需征得本站书面同意,否则视为侵权。本站信息为个人观点,若因引用本站信息而产生的任何情况,均与本站无关。Email:linmutou@163.com
闽ICP备14017840号-2