asp:textbox.text not displayed - c#

I have an asp:textbox with id=login
I am trying to display the user id retrieved from a post request:
protected void Page_Load(object sender, System.EventArgs e)
{
login.Text = Request["user"];
}
During debug, I can see that login.Text is populated with the user id, but the text is never displayed on the page.
Any help will be greatly appreciated!

try:
protected void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
login.Text = Request["user"];
}
}

Related

Clear content from fields after save .net c#

I have aspx form with textboxes and gridview.
When i insert data to textbox it's shows on the gridview after clicking "Save".
I need to clear the content of fieds after i click on "Save".
I tried alot of things and searched in google but nothing works.
I am new at .net.. Can someone help please?
This is my save button fuction:
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
grdvUsers.DataBind();
}
This is my pageLoad:
protected void Page_Load(object sender, EventArgs e)
{
grdvUsers.DataSource = Context1.sp_GetAllUserTest(); // select all users into gridview. datasorce = the data we want to dispaly.
grdvUsers.DataBind();
}
Simply make a method for clearing the controls that you want to clear:
private void ClearControls()
{
txtName.Text =""; // resetting textbox
txtEmail.Tex="";
txtMobile.Text ="";
ddlSomeDropDown.SelectedIndex = -1; // reset dropdown
somecheckBox.Checked = false; // reset checkbox
someRadio.Checked = false; // reset radio
..................
.................
// more controls here
}
and call it after saving :
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
grdvUsers.DataBind();
ClearControls();
}
Just assign text fields a empty string.
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
txtName.Text = String.Empty;
txtEmail.Text= String.Empty;
txtMobile.Text= String.Empty;
txtBirthdate.Text= String.Empty;
grdvUsers.DataBind();
}
You can also do this.
You can clear date and datepicker with DatetimePicker1.Clear(); or with the provided ID

Asp.net Session state Trying to keep contents of textbox when returned to page

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;
}
}

Why # not work in query string?

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"));

Dropdownlist to label output C#

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;
}

How can I redirect based on a selection of a DropDownList

I have a TextBox1 and a Search button in my application with this following code:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~\\searchpage.aspx?PatientNRIC=" + TextBox1.Text);
}
Which means, if the user type ONLY IC NO:S1234567D, then when click it will show the patient detailview.
So I now I'm doing almost the same thing but now I have a TextBox2 and a DropDownList1. Inside DropDownList1, I have "Name", "IC No", "Test_Date".
So for an example, I type "S1234567D" in the textbox1, and I choose "IC No" in DropDownList1 it should redirect me to a page of the S1234567D's patient detailview.
How could I do my code? Thanks!
Something like the following might work for you:
protected void Button1_Click(object sender, EventArgs e)
{
if(dropdownlist1.SelectedValue == "IC No")
{
// assuming this is the redirect to your patients details view
// but you MUST use only forward slashes to make it work (!)
Response.Redirect("~/searchpage.aspx?PatientNRIC=" + TextBox1.Text);
}
}
name.Text = ddl1.DataTextField;
ICNo.Text = ddl1.DataValueField;
textBox1.Text = name.text+ICno.Text;
protected void Button1_Click(object sender, EventArgs e)
{
if(textbox1 != null)
{
Response.Redirect("~/searchpage.aspx?PatientNRIC=" + TextBox1.Text);
}
}
}

Categories

Resources