在C#帮助中标记 [英] flag in c# help

查看:70
本文介绍了在C#帮助中标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用标志变量来标识同一按钮中的添加操作或更新操作.
根据标志值,它将执行添加或更新.

但是,每当我在asp页面中单击一个按钮时,标志值就会重新初始化.
我已经在页面加载(!Page.Ispostback)中使用过.请帮帮我.

提前谢谢.

Hi,

I am using flag variable to identify add operation or update operation in the same button.
Depending on the flag value it will perform add or update.

But whenever I click a button in asp page, flag value will be reinitialized.
I have used in page load (!Page.Ispostback). Please help me out.

Thanks in advance.

protected void Button1_Click(object sender, EventArgs e)
    {
        if (addupflag == 1)
        {
            Button1.Text = "Add";
            if ((TextBox1.Text == "") || (TextBox2.Text == ""))
            {
                Label4.Text = "User name and password can't be empty";
                TextBox1.Focus();
            }
            else
            {
                int cnt = ListBox1.Items.Count;
                dt1.Clear();
                dt1 = g1.Addusers(TextBox1.Text.ToString(), TextBox2.Text.ToString(), DropDownList1.SelectedValue);
                LoadAddresource(dt1);
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox1.Focus();
            }
        }
        else
        {
            dt1.Clear();
            dt1 = g1.Upadteusers(TextBox1.Text.ToString(), TextBox2.Text.ToString(), DropDownList1.SelectedValue);
            LoadAddresource(dt1);
            Button2.Enabled = false;
            TextBox1.Text = "";
            TextBox2.Text = "";
            Button1.Text = "Add";
            addupflag = 1;
            TextBox1.Enabled = true;
        }
    }


这是更新后的代码,其值应为1,但它应显示为"0".


this is the code after update its value should be 1 but it''l show "0"

推荐答案

您必须将标记值保存在会话中或隐藏字段中.
You must save the flag value in session or in a hiddenfield.


您可以将标志用作应用程序状态的一部分.
You could use the flag as part of the application state.


为什么不将标志变量声明为表单的私有全局变量,然后在
下的页面加载部分中将其值初始化 if(!Page.IsPostBack)属性.这样,将不会在调用button_click事件时重新初始化该标志.希望您能理解,我想告诉您.
Why dont you just declare the flag variable as a private global variable for the form, and initialize it value in the page load section under the
if(!Page.IsPostBack) property. This way, the flag will not be reinitialized on the calling of the button_click event. I hope you understand, what I am trying to tell.


这篇关于在C#帮助中标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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