Send data through the QueryString with ASP.NET - asp.net

I want to send a string to another page named Reply.aspx using the QueryString.
I wrote this code on first page that must send the text to Reply.aspx:
protected void FReplybtn_Click(object sender, EventArgs e)
{
String s = "Reply.aspx?";
s += "Subject=" + FSubjectlbl.Text.ToString();
Response.Redirect(s);
}
I wrote this code on the Reply.aspx page:
RSubjectlbl.Text += Request.QueryString["Subject"];
But this approach isn't working correctly and doesn't show the text.
What should I do to solve this?
Thanks

Though your code should work fine, even if the source string has spaces etc. it should return something when you access query string, please try this also:
protected void FReplybtn_Click(object sender, EventArgs e)
{
String s = Page.ResolveClientUrl("~/ADMIN/Reply.aspx");
s += "?Subject=" + Server.UrlEncode(FSubjectlbl.Text.ToString());
Response.Redirect(s);
}
EDIT:-
void Page_Load(object sender, EventArgs e)
{
if(Request.QueryString.HasKeys())
{
if(!string.IsNullOrEmpty(Request.QueryString["Subject"]))
{
RSubjectlbl.Text += Server.UrlDecode(Request.QueryString["Subject"]);
}
}
}
PS:- Server.UrlEncode is also sugested in comment to this question.

this is easy :
First page :
string s = "~/ADMIN/Reply.aspx?";
s += "Subject=" + FSubjectlbl.Text;
Response.Redirect(s);
Second page :
RSubjectlbl.Text = Request.QueryString["Subject"];

Related

IsPostBack doesn't work in ASP.NET

I want that when the page is first loaded , the string str is initialized with the text "I am here". And when the page is updated by clicking on the button (btn_click) the value is the same that was initialized. But it does not work. In the console I reads:
First time I load the page: "I ​​am here"
1.When I click on the button: "empty"
2.And I think we should keep this value: "I am here". Please help.
public partial class Default : System.Web.UI.Page
{
string str = "empty";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
str = "I am here";
Debug.Write("VALUE: " + str + "\r\n");
}
else
{
Debug.Write("VALUE: " + str + "\r\n");
}
}
protected void btn_Click(object sender, EventArgs e)
{
//do something...
}
}
Each individual request to the server creates a new instance of the page... both the very first request (IsPostBack==false) and all subsequent requests (IsPostBack==true).
This means that your str variable is initialised with "empty" every time you request the page, but only ever set to "I am here" on the first load (i.e. the IsPostBack==false). You need to store it somehow so that when the post-back occurs you still have it.
There are several ways, including using the Session object, but I would suggest using the ViewState like this...
string str = "empty";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
str = "I am here";
ViewState["str"] = str;
Debug.Write("VALUE: " + str + "\r\n");
}
else
{
str = (string)ViewState["str"];
Debug.Write("VALUE: " + str + "\r\n");
}
}

Sending values from one page to another using sessions

I am sending values from send page to receive page using sessions, its working fine but the problem is I remember the URL and i directly run receive page its shows previous information but it's invalid is it....?
If end user directly visit receive page i need to restrict or give some message something like that
How can i overcome this problem..............
This is my code
send.aspx.cs
protected void gv_rowcommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource)).NamingContainer;
Session["c"] = grow.Cells[0].Text;
Session["s"] = grow.Cells[1].Text;
Session["e"] = grow.Cells[2].Text;
Session["t"] = grow.Cells[3].Text;
Session["a"] = grow.Cells[4].Text;
Response.Redirect("confirmation.aspx");
}
}
confirmation.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
lblgetcourse.Text = Convert.ToString(Session["c"]);
lblgetstartdate.Text = Convert.ToString(Session["s"]);
lblgetenddate.Text = Convert.ToString(Session["e"]);
lblgettimings.Text = Convert.ToString(Session["t"]);
lblgetamount.Text = Convert.ToString(Session["a"]);
}
Once you have displayed the values on the confirmation page you could remove them from the Session.
Session.Remove("c");
...
This will ensure that the confirmation page will always display fresh values coming from the send.aspx page. You could also check for the presence of those values in the session before displaying them.
On Confirmation Page, Fetch the Values from Session Variables an display them , then Clear Session variables just like: Session.Clear();
You can check the referrer header :
Request.UrlReferrer
and see where the request comes from.
so your page_load will look like :
protected void Page_Load(object sender, EventArgs e)
{
if (!Request.UrlReferrer=='...') Response.Redirect("~/login.aspx");
...
protected void Page_Load(object sender, EventArgs e)
{
if(Request.UrlReferrer.AbsolutePath == "~/send.aspx")
{
lblgetcourse.Text = Convert.ToString(Session["c"]);
lblgetstartdate.Text = Convert.ToString(Session["s"]);
lblgetenddate.Text = Convert.ToString(Session["e"]);
lblgettimings.Text = Convert.ToString(Session["t"]);
lblgetamount.Text = Convert.ToString(Session["a"]);
}
else
{
//do something
}
}

asp.net textbox not updated from another task

I have a GridView and on its SelectedIndexChanged the code is fired:
protected void grdEntry_SelectedIndexChanged(object sender, EventArgs e)
{
lblAssignId.Text = grdEntry.SelectedRow.Cells[1].Text == " "
? ""
: grdEntry.SelectedRow.Cells[1].Text;
Ob.BranchId = Globals.BranchID;
Ob.AssignId = lblAssignId.Text;
DataSet dsMain = GetAssignDetails(Ob);
if (dsMain.Tables[0].Rows.Count != 0)
{
// some other code
Task.Factory.StartNew(() => FillMemberShipAndBarCode(dsMain.Tables[0].Rows[0]["CustomerCode"].ToString(), Ob.BranchId));
}
}
and the code for filling membership id is
private void FillMemberShipAndBarCode(string customerCode, string branchId)
{
var sqlCommand = new SqlCommand
{
CommandText = "sp_customermaster",
CommandType = CommandType.StoredProcedure
};
sqlCommand.Parameters.AddWithValue("#CustomerCode", customerCode);
sqlCommand.Parameters.AddWithValue("#BranchId", branchId);
sqlCommand.Parameters.AddWithValue("#Flag", 18);
var data = PrjClass.GetData(sqlCommand);
txtMemberShip.Text = data.Tables[0].Rows[0]["MembershipId"].ToString();
txtBarCode.Text = data.Tables[0].Rows[0]["Barcode"].ToString();
}
It's working fine, but is is not updating any of the textboxes. Also, I checked in watch window, the values are returned as expected (M-1213 and 634-98-4 ) and the code does reaches the point txtMemberShip.Text = data.Tables[0].Rows[0]["MembershipId"].ToString();
but the txtMemberShip just remains empty??? Can anyone tell me why is not updating the textboxes?
UPDATE
As per comments, here is the page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDropDown();
BindGrid();
SetDefaultsInTextBoxes();
}
}
And I don't have any code that waits on this task.
Don't do this:
Task.Factory.StartNew(() => FillMemberShipAndBarCode(dsMain.Tables[0].Rows[0]["CustomerCode"].ToString(), Ob.BranchId));
What are you trying to achieve by doing so?
What is probably happening is your method FillMemberShipAndBarCode is probably running after ASP.NET has already sent the page back to the browser. Thus, essentially, no visible effect on the rendered HTML.
ASP.NET isn't a good place to do multi-threaded stuff.
Try just replacing the above with this:
FillMemberShipAndBarCode(dsMain.Tables[0].Rows[0]["CustomerCode"].ToString(), Ob.BranchId);

Passing the value of a XML tag to another page

Here is the page where I am retrieving from a XML page and by storing it in cookie, I want to retrieve it in another page.
public partial class shopping : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie userCookie = new HttpCookie("user");
userCookie["quantity"] = TextBox1.Text;
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("shopping_cart.xml"));
XmlNode root = doc.DocumentElement;
if (RadioButton1.Checked)
{
string str1 = doc.GetElementsByTagName("cost").Item(0).InnerText;
userCookie["cost"] = str1;
//Label3.Text = str1;
Response.Redirect("total.aspx");
}
}
}
and here is other page where I am trying to retrieve it (total.aspx.cs):
public partial class total : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
**Label2.Text = Request.Cookies["user"]["quantity"];**
}
}
I am getting a Null Reference on the line which is in bold. Any suggestions on how can I do it?
You created the cookie in the first section, but forgot to append it to the Response.
Response.Cookies.Add(userCookie); // place before your Response.Redirect
Also, be aware that cookies have a useful maximum size of 4000 bytes, and otherwise a probably not the best choice for what you are doing. You may wish to store temporary session info in the Session for access between pages, rather than use a cookie.
Session["quantity"] = TextBox1.Text
// ...
Session["cost"] = str1;
and in the second page
Label2.Text = Session["quantity"] as string;

how to display the mark sheet on the next page using ASP.NET?

i want to creat a web page that uses radiobuttons in a multiple choice quetions, but i want to do is to use a submit button to return the results to the next page with the mark sheet, so can you please help me with that. how to display the mark sheet on the next page using ASP.NET
Use Session state.
For example,
protected void Button1_Click(object sender, EventArgs e)
{
Session["key"] = TextBox1.Text;
Response.Redirect("marksheet.aspx");
}
Code in Page_Load of marksheet.aspx,
protected void Page_Load(object sender, EventArgs e)
{
if (Session["key"] != null)
{
object val = Session["key"];
...
}
}

Resources