asp.net按钮必须两次发射 [英] asp.net Button needs to be fired twice

查看:159
本文介绍了asp.net按钮必须两次发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成动态的HTML控件。控件上正在初始化生成,用户已引起pressing一个按钮回发后。但是,他需要使生成的控件两次preSS它。我该如何解决这个问题? code:

 保护覆盖无效的OnInit(EventArgs的发送)
    {
        如果(Page.IsPostBack)
        {
            如果(会话[ID]!= NULL)
            {
                字符串ID =会话[ID]的ToString()。
                GenerateDynamicControls(ID);            }
        }
    } 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        会话[ID] = NULL;
    }保护无效的button1_Click(对象发件人,EventArgs的发送)
    {
        字符串ID = TextBox1.Text;
        会话[ID] = ID;
    }


解决方案

有没有必要使用会话为这些目的。再加上你的code可能随后回传后失败。

 保护覆盖无效LoadViewState(对象状态)
{
    base.LoadViewState(州);
    VAR ID = this.ViewState [DynamicControlGeneration]作为字符串;
    如果(ID!= NULL)
        GenerateDynamicControls(ID);
}保护无效的button1_Click(对象发件人,EventArgs的发送)
{
    字符串ID = TextBox1.Text;
    this.ViewState [DynamicControlGeneration] = ID;
    GenerateDynamicControls(ID);
}

I am generating dynamic html controls. The controls are being generated on Init, after the user has caused a postback by pressing a button. However, he needs to press it twice so that the controls are generated. How can I fix this? Code:

 protected override void OnInit(EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (Session["id"] != null)
            {
                string id= Session["id"].ToString();
                GenerateDynamicControls(id);

            }
        }
    }

 protected void Page_Load(object sender, EventArgs e)
    {
        Session["id"] = null;
    }

protected void Button1_Click(object sender, EventArgs e)
    {
        string id = TextBox1.Text;
        Session["id"] = id;
    }

解决方案

There is no need to use session for these purposes. Plus your code might fail after subsequent postbacks.

protected override void LoadViewState(object state)
{
    base.LoadViewState(state);
    var id = this.ViewState["DynamicControlGeneration"] as string;
    if (id != null)
        GenerateDynamicControls(id);
}

protected void Button1_Click(object sender, EventArgs e)
{
    string id = TextBox1.Text;
    this.ViewState["DynamicControlGeneration"] = id;
    GenerateDynamicControls(id);
}

这篇关于asp.net按钮必须两次发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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