dynamic gridview caption, c# codebehind

The attribute caption="Students" need to be dynamic based on session variable Session["type"] from event in code behind (grdHighLight_RowDataBound()).

Session["type"] = 1 -->  caption="Students"
Session["type"] = 2 -->  caption="Teacher"
Session["type"] = 3 -->  caption="Parent"

Question: How could this be done?

Important: Although a gridview tag included with the aspx file but it gets populated on the run via LoadData() event in load event.

Thank you.
   <div>
------------------------- aspx -----------------------                                   
   <asp:GridView runat="server" ID="grdHighLight"  caption="Students" onrowdatabound="grdHighLight_RowDataBound" CellSpacing="-1" GridLines="None" >
            
    </asp:GridView>

------------------------- aspx.cs ------------------
public partial class MasterPage2 : System.Web.UI.MasterPage
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ROD_July18ConnectionString"].ToString());
    RodCriteria rc = new RodCriteria();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["StatType_ID"] = rc.ReadCriteria("StatIdS");
            Session["YYYY"] = rc.ReadCriteria("YYYY");
            Session["RegionID"] = rc.ReadCriteria("RegionId");
            Session["StatS"] = rc.ReadCriteria("StatS");
            LoadData();
            //GridView gview = new GridView();
            //gview = (GridView)Page.FindControl("grdHighLight");
            //gview.Caption = "<span style='color:Green;'>" + gview.Caption + "</span>";
        }
    }
    private GridView setupGridView(string caption, GridView gview)
    {

    private void LoadData()
    {
        SqlCommand cmd = new SqlCommand("spAuunuaCountMedCtr", con);
        cmd.Parameters.AddWithValue("@RegionID", Session["RegionID"]);
        cmd.Parameters.AddWithValue("@YYYY", Session["YYYY"]);
        cmd.Parameters.AddWithValue("@StatType_ID", Session["StatType_ID"]);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            con.Open();
            grdHighLight.EmptyDataText = "No Records Found";
            grdHighLight.DataSource = cmd.ExecuteReader();
            grdHighLight.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    protected void grdHighLight_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // this event or elsewhere. This event is better because I can have more control when 
        // there are two grid views on a single form.

        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.CssClass = "headerBracketGround";
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.CssClass = "highlightBracketground";
            var val = e.Row.Cells[2].Text;
            var snglVal = Single.Parse(val);
            e.Row.CssClass = snglVal > 0.0 ? "highlightBracketground" : "bracketground";
        }
    }
}

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
Quick and dirty way to do it:
grdHighLight.Caption = new string[] { "Students", "Teacher", "Parent" }[(int)Session["type"] - 1];

Open in new window

Obviously there are better ways to define the array, either in the class, get it from settings or a database table etc.

Also a check to make sure the Session variable contains a sane value should be added although you may already have that at the point where it's being set.

PS: I don't see why this would be done in the DataBound event, would say stick it in LoadData.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.