0

How can I access variables on page_load and use it on ddlApp_SelectedIndexChanged method in c#? thank you

private void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlRole.SelectedIndex = 0;
    }

    string ddl = ddlApp.Value.ToString();
    string ddlRoleDs;
    string ddlMenuDs;
    string GvDs;

    if (ddl == "ATTD")
    {
        ddlRoleDs = "ddlAttdDs";
        ddlMenuDs = "ddlMenuAttdDs";
        GvDs = "AttdMenuAssignmentDs";                
    }
    else if (ddl == "TRVL")
    {
        ddlRoleDs = "ddlTrvldDs";
        ddlMenuDs = "ddlMenuTrvlDs";
        GvDs = "TrvlMenuAssignmentDs";                
    }

}

the variable: ddlRoleDs, GvDs and ddMenuDs

protected void ddlApp_SelectedIndexChanged(object sender, EventArgs e)
{        
    ddlRole.DataSourceID = ddlRoleDs;
    MenuAssignmentGv.DataSourceID = GvDs;
    ddlMenu.DataSourceID = ddlMenuDs;
}
Esteban Verbel
  • 738
  • 2
  • 20
  • 39
patrixx
  • 11
  • 6

1 Answers1

0

You can use private global members in your class. This way they can only be accessed from within the class. The code below shows you exactly how to declare and use them

private string ddlRoleDs;
private string GvDs;
private string ddMenuDs;

private void Page_Load(object sender, EventArgs e)
{
    // don't declare them here, but you can use them here
    ddlRoleDs = "test value";
    ....
}

After the values were set in the Page_load method, they can be used in your other method

protected void ddlApp_SelectedIndexChanged(object sender, EventArgs e)
{        
    ddlRole.DataSourceID = ddlRoleDs;
    MenuAssignmentGv.DataSourceID = GvDs;
    ddlMenu.DataSourceID = ddlMenuDs;
}
Esteban Verbel
  • 738
  • 2
  • 20
  • 39
  • thank you, it works now. but seem it doesn't work in Gridview event handler. i use the variable to set data source of a ComboBox datasourceID in a gridview. – patrixx Dec 19 '16 at 03:05
  • that's another question, you can find the answer to that here: http://stackoverflow.com/q/5976511/6198927. Don't forget to mark your question as answered – Esteban Verbel Dec 19 '16 at 13:43
  • thank you. how to mark question as answered? sorry i still new in stackoverflow – patrixx Dec 20 '16 at 00:19