Monday, 6 August 2012

Example of view state,query string

Use of View State,Query String, Cookies and about all state management I described in my earlier post. Here, now I m giving some example state management.....

Train No.        textbox1
Class               textbox2

viewstatebutton   querystringbutton   cookiesbutton  showviewstate

// In code we have to write like this
//on click of Query String Button

protected void Btn_qstring_Click(object sender, EventArgs e)
    {
//gv.aspx is name of next page in which we want to go with our data.


        Response.Redirect("gv.aspx?Train No.=" +
this.Txt_tno.Text + "&Class=" +
this.Txt_class.Text);


or,



        Server.Transfer("gv.aspx?Traininfo=" + Txt_tno.Text + Txt_class.Text);
}


and on gv.aspx(where we want to transfer/display this data)



TextBox1.Text = "Train Information is:";
            this.TextBox1.Text = Request.QueryString["Train No."];
            this.TextBox2.Text = Request.QueryString["Class"];
or,
           this.TextBox1.Text = Request.QueryString[0];
           this.TextBox2.Text = Request.QueryString[1];


// any of the above of both codes


For View State

//code will save data in view state

protected void Btn_Vstate_Click(object sender, EventArgs e)
    {
        string data = "Train No.:" + Txt_tno.Text + "class:" + Txt_class.Text;
        ViewState["Train Data"] = data;
    }

//code will show data from view-state

    protected void Btn_showvsv_Click(object sender, EventArgs e)
    {
        string d=(string)ViewState["Train Data"];
        Label3.Text = "ViewState Data:" + d;
    }

For Cookie

protected void cookiesbutton_Click(object sender, EventArgs e)
    {
//for save data in cookie

        Response.Cookies["StudentCookies"].Value = TextBox1.Text;
        Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);

// for show/read data from cookie
string roll=Request.Cookies["StudentCookies"].Value;
        Label1.Text = roll;
    }








I will update more examples very soon.......If you have any problem regarding this code please give me your reply and suggestion. You can ask any query from me.

Thanks
Amitesh Anand
Software Developer

No comments:

Post a Comment