In cs.aspx page i have a button with following code:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/cs.aspx?p=ali#25");
}
In page_load i get query string and display it:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["p"] != null)
{
string p = Request.QueryString["p"];
Response.Write("p= "+p);
}
}
in query string:
p = ali#25
but in run time display
p = ali
why string after # not shown.
found a solution. use Server.UrlEncode:
Response.Redirect("~/cs.aspx?pass="+Server.UrlEncode("a#25"));
Related
I have a textbox. In that textbox I write Human. Then I click a button, and if the word is human, then on a richtextbox, the word human will appear.
Here's the code I've tried.
private void button3_Click(object sender, EventArgs e)
{
if (textBox4.Text)
{
richTextBox1.Text = "human";
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
string value = textBox4.Text;
}
I tried making the textbox into a string so I could use it in the if statement, but it didn't work, so instead I used texbox4.text, but it is still wrong.
You could simply do with this piece of code,
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text =="human")
{
richTextBox1.Text = textBox1.Text;
}
}
I have allready asked this question before, but now I have found more details and some more weird stuf.
I have a web project that has 2 .aspx files that connect data with sessions. Without any extra code this works, when i add my extra code the session no longer works. Does anybody have an idea why?
Code where sessions work:
Form1:
protected void Page_Load(object sender, EventArgs e)
{
Session["data"] = "5";
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // 5 when i look while debugging
Response.Redirect("~/Form2.aspx", false);
}
Form 2:
protected void Page_Load(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = 5 when i look while debugging
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = 5 when i look while debugging
}
Code where sessions don't work: (i use 2 classlibraries (logic and dataAccess where I get data from json webservice and parse it to my forms).
Form1:
protected void Page_Load(object sender, EventArgs e)
{
Logic logic = new Logic();
logic.login(credentials);
List<AppointmentExtParticipant> opleidingVolgers = logic.getOpleidingVolgers();
foreach (AppointmentExtParticipant app in opleidingVolgers)
{
if (app.contact != null)
{
Relation rel = logic.getRelationData(app.contact.FK_RELATION);
DropDownListUsers.Items.Add(app.ToString() + " " + rel.ToString());
}
}
Session["data"] = "5";
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["opleidingvolger"]); // s = 5 when i look while debugging
Response.Redirect("~/Form2.aspx", false);
}
Form 2:
protected void Page_Load(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = null when i look while debugging
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = null when i look while debugging
}
Ofcourse I simplified the names here a bit so people could understand, thnx!
edit:
logic login:
dataaccess login:
Here i get data from my webservice hosted on another url.
Sessions generally work by using cookies.You are using HttpWebRequests which creates new sessionSo you need to preserve session cookie between requests. Thats why we use CookieContainer so add
CookieContainer container = new CookieContainer();
HttpWebRequest req = WebRequest.Create(
"") as HttpWebRequest;
req.CookieContainer = container;
My problem is that I can get from page_load to show_books function
but I cant get from show_books to book_detail.
Every time I try to fire book_details it gets me to page_load.
my code is this:
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(book_show);
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(book_details);
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
I'm sorry if my question is annoyingly newbie
but I would like some help .
I just started to learn asp.net
well i don't understand why you need to do this,but code below works,hope it helps
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(this.book_show);
lnk_button.CommandName = "testClick";
lnk_button.CommandArgument = "1";
lnk_button.ID = "11";
lnk_button.Text = "blah";
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
public void book_show()
{
book_details() ;
}
public void book_details()
{
}
ive two pages, one with a text box and a button, the other with a button a label. What i want to do is to display contents of the textbox on page 1, in the label of the page2 on button click. and then when i click the button to return to page1. preverse whats entered in the textbox on page1. sorry if its confusing. heres my code
page1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
Session["fstName"] = txtBox.Text;
Response.Redirect("Page2.aspx");
}
page2.aspx
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["fstName"].ToString();
lblPage2.Text = a;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}
Where do you set the value of the text box when returning to WebForm1.aspx? It should be very similar to what you have for the label on Page2.aspx. Something like:
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["fstName"].ToString();
txtBox.Text = a;
}
At worst, you may need to wrap some error checking around it. Maybe something like:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
if (Session["fstName"] != null)
{
string a = Session["fstName"].ToString();
txtBox.Text = a;
}
}
I want to show different labels output from DropDownList with button
select Item on DropDownList and click button to show output in label
Can anyone help me out on this?
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedValue;
}
just write down this in button click event
protected void Button1_Click(object sender, EventArgs e) {
label.text = ComboBox.SelectedText;
}
protected void Button1_Click(object sender, EventArgs e)
{
lable1.Text=DropDownList1.SelectedValue.ToString();
}
or u can do
protected void Button1_Click(object sender, EventArgs e)
{
String input=DropDownList1.SelectedIndex >= 0 ? DropDownList1.SelectedItem.ToString() : "";
lable1.Text=input;
}