创建GlobalVariable并在另一个按钮单击事件中使用它 [英] Create GlobalVariable and use it in another button click event

查看:73
本文介绍了创建GlobalVariable并在另一个按钮单击事件中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱所有我想要如何创建GlobalVariable并将我的数据库名称保存在此变量中,以便在页面的任何位置使用参考。

我的代码如下所示,每个东西都很完美但不定义GlobalVariable如果你的人知道请帮助我这是非常紧急的

 受保护的  void  Button3_Click ( object  sender,EventArgs e)
{
string str = .Empty;
DataSet ds = new DataSet();
string db = ;

foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
if (CheckBoxList1 .SelectedIndex!= 0
{
if (CheckBoxList1.SelectedItem.Value == arjun
{
db = arjun;

}
if (CheckBoxList1.SelectedItem.Value == bsales
{
db = bsales;
}
if (CheckBoxList1.SelectedItem.Value == hr
{
db = HR;
}
if (CheckBoxList1.SelectedItem.Value == information_schema
{
db = INFORMATION_SCHEMA;
}
if (CheckBoxList1.SelectedItem.Value == intall
{
db = intall;
}
if (CheckBoxList1.SelectedItem.Value == 消息
{
db = 消息;
}
if (CheckBoxList1.SelectedItem.Value == mysql
{
db = MySQL的;
}
if (CheckBoxList1.SelectedItem.Value == sales
{
db = 销售;
}

}
string abc = Data Source = localhost; Initial Catalog = + db + ; user id = root; password = root;

MySqlConnection myconn = new MySqlConnection(abc);
myconn.Open();
str = li.ToString();
string query = show tables from + str + ;
MySqlDataAdapter mydata = new MySqlDataAdapter(query,myconn);
mydata.Fill(ds,str);
myconn.Close();


}

int k = 0 < /跨度>;
foreach (DataTable dt in ds.Tables)
{
if (k == 0
{
CheckBoxList2.DataSource = ds。表[ 0 ];
CheckBoxList2.DataTextField = tables_in _ + ds.Tables [ 0 ]。TableName + ;
CheckBoxList2.DataValueField = tables_in _ + ds.Tables [ 0 ]。TableName + ;
CheckBoxList2.DataBind();
}
如果(k == 1
{
CheckBoxList3.DataSource = ds.Tables [ 1 ];
CheckBoxList3.DataTextField = tables_in _ + ds.Tables [ 1 ]。TableName + ;
CheckBoxList3.DataValueField = tables_in _ + ds.Tables [ 1 ]。TableName + ;
CheckBoxList3.DataBind();
}
k ++;
}
}
}

解决方案

试试这个。 :)



如果在session_start()方法的事件中创建变量,它将只在该方法内可用。 

如果在应用程序或会话状态中存储值,则可以在整个应用程序中访问这些值。例如:





  string  myAppTitle =( string )应用程序[  appTitle] '   





参考.. :)



应用程序变量 [ ^ ]


  public   partial   class  ClassName:System.Web.UI.Page 
{
string str = ; // global您可以为th赋值可在页面上的任何位置变量

// 其他事件.... 。
}


创建一个类并拥有静态只读属性,例如



  public   class  GlobalClass 
{< span class =code-keyword> public static string ConnectionString { get { return 您的连接字符串 // 您也可以阅读表单配置..
}}
}





和所有页面都可以这样访问

  string  conn = GlobalClass.ConnectionString; 





根据评论更新:



< pre lang =c#> public class GlobalClass
{

public static 字典< string,string> ConnectionString
{
get
{
Dictionary< string,string> dict = new 字典< string,string>();
dict.Add( Con1 您的连接字符串1);
dict.Add( Con2 您的连接字符串2);
dict.Add( Con3 您的连接字符串3);
dict.Add( Con4 您的连接字符串4);
return dict;
}
}
}





您可以将其作为
$ b访问$ b

  string  con1 = GlobalClass.ConnectionString [   Con1]; 
string con2 = GlobalClass.ConnectionString [ CON2\" ];
string con3 = GlobalClass.ConnectionString [ CON3\" ];
string con4 = GlobalClass.ConnectionString [ CON4\" ];


Dear All i want how to Create GlobalVariable and save my database name in this variable for use reference anywhere in page.
My code are as below in this code every thing is perfect but not define GlobalVariable if you people know pls help me it is very urgent

protected void Button3_Click(object sender, EventArgs e)
    {
        string str = string.Empty;
        DataSet ds = new DataSet();
        string db = "";

        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected)
            {
                if (CheckBoxList1.SelectedIndex != 0)
                {
                    if (CheckBoxList1.SelectedItem.Value == "arjun")
                    {
                        db = "arjun";

                    }
                    if (CheckBoxList1.SelectedItem.Value == "bsales")
                    {
                        db = "bsales";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "hr")
                    {
                        db = "hr";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "information_schema")
                    {
                        db = "information_schema";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "intall")
                    {
                        db = "intall";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "messages")
                    {
                        db = "messages";
                    }
                    if(CheckBoxList1.SelectedItem.Value=="mysql")
                    {
                        db = "mysql";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "sales")
                    {
                        db = "sales";
                    }
                
                }
                string abc = "Data Source=localhost;Initial Catalog=" + db + ";user id=root;password=root";
               
                MySqlConnection myconn = new MySqlConnection(abc);
                myconn.Open();
                str = li.ToString();
                string query = "show tables from " + str + "";
                MySqlDataAdapter mydata = new MySqlDataAdapter(query, myconn);             
                mydata.Fill(ds, str);              
                myconn.Close();
                
                
            }

            int k = 0;
            foreach (DataTable dt in ds.Tables)
            {
                if (k == 0)
                {
                    CheckBoxList2.DataSource = ds.Tables[0];
                    CheckBoxList2.DataTextField = "tables_in_" + ds.Tables[0].TableName + "";
                    CheckBoxList2.DataValueField = "tables_in_" + ds.Tables[0].TableName + "";
                    CheckBoxList2.DataBind();
                }
                if (k == 1)
                {
                    CheckBoxList3.DataSource = ds.Tables[1];
                    CheckBoxList3.DataTextField = "tables_in_" + ds.Tables[1].TableName + "";
                    CheckBoxList3.DataValueField = "tables_in_" + ds.Tables[1].TableName + "";
                    CheckBoxList3.DataBind();
                }
                k++;
            }
        }   
    }

解决方案

try thiss. :)

If you create a variable inside the event of the session_start() method, it will only be available inside that method.

If you store a value in application or session state, those are accessible throughout your whole application. For example:



string myAppTitle = (string)Application["appTitle"]'



reference.. :)

Application Variables[^]


public partial class ClassName : System.Web.UI.Page
{
  string str="";//global  You can assign a value to this variable anywhere on the page

  //other Events.....
}


Create a class and have a static Read only property like

public class GlobalClass
{    public static string ConnectionString { get { return "Your connection string" // you can read form config also..
 } }
}



and in the all pages you can access like this

string conn = GlobalClass.ConnectionString;



updated based on comments:

public class GlobalClass
{

    public static Dictionary<string, string> ConnectionString
    {
        get
        { 
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Con1", "Your connection string 1");
            dict.Add("Con2", "Your connection string 2");
            dict.Add("Con3", "Your connection string 3");
            dict.Add("Con4", "Your connection string 4");
            return dict;
        }
    }
}



and you can access it as

string con1 = GlobalClass.ConnectionString["Con1"];
       string con2 = GlobalClass.ConnectionString["Con2"];
       string con3 = GlobalClass.ConnectionString["Con3"];
       string con4 = GlobalClass.ConnectionString["Con4"];


这篇关于创建GlobalVariable并在另一个按钮单击事件中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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