How can I display User Input to another page - asp.net

How can I display user input if I have this example.
(Default.aspx)
FirstName: (textbox)
Last Name: (textbox)
I wanted to display the user input coming from Default.aspx to another page (WebForm1.aspx)
using Response.Redirect.
How can I do this ?
Is
Response.Redirect("WebForm1.aspx?VariableName" + TextBox1.Text)
and TextBox1.Text.Request.QueryString("VariableName")
working ?

Yes, it will work but you need to add = after your variable name as below
Response.Redirect("WebForm1.aspx?VariableName1=" + TextBox1.Text + "&VariableName2="+ TextBox2.Text);
and also in yourWebForm1.aspx
TextBox1.Text = Request.QueryString["VariableName"];
TextBox2.Text = Request.QueryString["VariableName2"];

Page one:
protected void Button1_Click(object sender, EventArgs e)
{
String strTxt1 = TextBox1.Text.ToString();
String strTxt2 = TextBox2.Text.ToString();
Response.Redirect("~/new_name.aspx?fName=" + strTxt1 + "&lName=" + strTxt2);
}
Page two:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["fName"].ToString();
Label2.Text = Request.QueryString["lName"].ToString();
}

Related

second page does not fetch correct data in C#

First page :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[0];
firstCell.Controls.Clear();
firstCell.Controls.Add(new HyperLink { NavigateUrl = "ser_job_status1.aspx?Complaint_No = " + firstCell.Text, Text = firstCell.Text, Target = "_blank" });
Session["Complaint_No"] = firstCell.Text;
}
}
Second page :
protected void Page_Load(object sender, EventArgs e)
{
string strComplaintNo = Convert.ToString(Session["Complaint_No"]);
TextBox51.Text = strComplaintNo;
}
TextBox.51.Text of my second page fetches value from First page session.
My problem is my textbox51.text fetches value of Complaint_No which is bound finally to gridview rather fetching my hyperlinked value..
Kindly help to solve this issue..
Session["Complaint_No"] = firstCell.Text; is set in your session, so value of firstCell.Text must be fetched in the second page.
change
Session["Complaint_No"] = firstCell.Text;
to
Session["Complaint_No"] = ((HyperLink)firstCell.Controls[0]).NavigateUrl;

Call a button_click on page_load asp.net

I have a search textbox and a search button, when clicked displays a grid with the following names and gender.However I have redirected the page to another page on edit.Now When I comeback from that page to the page containing the gridview I want to display the same search again. I have successfully put retrieved the information but storing it into session, but I'm not able to call my btn_click event # page_Load.
Here's a snippet:
EDIT: I have made some changes in my code
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Redirected"] != null)
{
if (Session["FirstName"] != null)
txtSearch.Text = Session["FirstName"].ToString();
if (Session["Gender"] != null)
ddlGen.SelectedValue = Session["Gender"].ToString();
btnSearch_Click(sender, e);
}
if (!Page.IsPostBack)
{
BindGrid();
}
}
and here's the click event:
protected void btnSearch_Click(object sender, EventArgs e)
{
string query = "Select EmployeeId,FirstName,Password,Address,sex,Deptno,act_book,actTV,DOJ,isActiveYN from employees where 1=1";
if (txtSearch.Text != "")
{
query += " and FirstName like '%" + txtSearch.Text + "%'";
Session["FirstName"] = txtSearch.Text;
}
if (ddlGen.SelectedValue != "")
{
query += " and sex='" + ddlGen.SelectedValue.ToUpper() + "'";
Session["Gender"] = ddlGen.SelectedValue;
}
DataSet ds = new DataSet("Employees");
SqlConnection con = new SqlConnection("Password=admin;User ID=admin;Initial Catalog=asptest;Data Source=dbsvr");
SqlDataAdapter da = new SqlDataAdapter(query, con);
da.Fill(ds);
gvSession.DataSource = ds;
gvSession.DataBind();
}
Now I'm able to save search, so that problem is resolved ,but another has poped up that when I click the button search after changin text it takes me back to the older search..The reason is probably because sessions are not cleared,but I did that as well by handling textchanged and selectedindexchanged eventd.
Rather than trying to call your button click handler from the Page_Load, change your button click handler to simply call another method like:
protected void btnSearch_Click(object sender, EventArgs e)
{
RunSearch();
}
Then move all your btnSearch_Click() code into RunSearch()
Then in your Page_Load you can do something like:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Gender"] != null && Session["FirstName"] != null)
{
txtSearch.Text = Session["FirstName"].ToString();
ddlGen.SelectedValue = Session["Gender"].ToString();
RunSearch();
}
if (!Page.IsPostBack)
{
BindGrid();
}
}
On a side note, I would recommend taking a look into SQLCommand Parameters. Your code is prone to SQL Injection Attacks:
http://en.wikipedia.org/wiki/SQL_injection
You should reset the session redirected variable so it doesn't fall in the same case.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Redirected"] != null)
{
Session["Redirected"] = null;
....
You can do using an QueryString paremeter when page return back to main page then here you can check QueryString paremeter is exist. here you can implement code for bind grid
if (Request.QueryString["Back"]!= null)
{
// Your bind grid function
}
You can create a function, which will called both from the button_click and page_load.

problem in updating the textbox data into SQLSERVER

I have problem in updating user profile through asp:TextBox.
At the time of page load , all the textboxes get loaded with correct values from the database sqlserver.
say First name=SAM {i.e SAM is shown in txtName's textfield)
and Last Name=Berton
but when i alter the content of txtName to SAMANTHA
and similary for other textboxes, and click on update profile button, Internally txtName.text="SAM" gets retained notwithstanding the changed content of txtName.text="SAMANTHA" by manually altering the content of textbox.So same value that is "SAM" gets stored into the sqlserver rather than "SAMANTHA".
protected void Page_Load(object sender, EventArgs e)
{
loadProfileData();
}
protected void loadProfileData()
{
string connStringProfileload = ConfigurationManager.ConnectionStrings["myconnString"].ConnectionString;
SqlConnection conProfLoad = new SqlConnection(connStringProfileload);
conProfLoad.Open();
string emailAddLogin = User.Identity.Name.ToString();
string strSqlProfileLoad = "SELECT * FROM [gen_profile] WHERE [email]=#email";
SqlCommand cmd = new SqlCommand(strSqlProfileLoad, conProfLoad);
cmd.Parameters.AddWithValue("#email", emailAddLogin);
SqlDataReader drProfileLoad = cmd.ExecuteReader();
while (drProfileLoad.Read())
{
txtName.Text = drProfileLoad["fname"].ToString();
txtLastName.Text=drProfileLoad["lname"].ToString();
}
drProfileLoad.Close();
conProfLoad.Close();
}
protected void BtnUpdtProf_Click(object sender, EventArgs e)
{
string connStringProfileUpdate = ConfigurationManager.ConnectionStrings["myconnString"].ConnectionString;
SqlConnection conUpdateProf = new SqlConnection(connStringProfileUpdate);
conUpdateProf.Open();
string emailAddLogin = User.Identity.Name.ToString();
string strSqlUpdateProf = "UPDATE gen_profile SET fname =#fname, lname =#lname where email=#email;";
SqlCommand cmdUpdate = new SqlCommand(strSqlUpdateProf, conUpdateProf);
cmdUpdate.Parameters.AddWithValue("#fname", txtName.Text.ToUpper().Trim());
cmdUpdate.Parameters.AddWithValue("#lname", txtLastName.Text.ToUpper().Trim());
cmdUpdate.Parameters.AddWithValue("#email", emailAddLogin);
int i = cmdUpdate.ExecuteNonQuery();
conUpdateProf.Close();
if (i == 1)
Response.Redirect("~/prof/resp_update_prof.aspx");
}
Change this method :
protected void Page_Load(object sender, EventArgs e) {
loadProfileData();
}
to :
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) loadProfileData();
}
Basically, your textBox values are being reset in the page load event, so everytime your page posts back, it's resetting the values in the textboxes and then saving them back to the database.
This is because before your button click event, page_load get's fired which re-populates the textbox.
You can stop this happening by checking for a post back as follows:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
{
loadProfileData();
}
}

Datakeynames, request.querystring

I am doing something wrong in this small code
Page1: `
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
UserType = DDlUserType.SelectedItem.Text;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the value in the hyperlink column.
string HyperLinkValue = e.Row.Cells[1].Text;
HyperLink myLink = new HyperLink();
myLink.NavigateUrl = "~/ShowMMBProfileStats1.aspx?Profile_ID={0}";
myLink.Text = HyperLinkValue;
}
In ShowMMBProfileStats1.aspx
protected void Page_Load(object sender, EventArgs e)
{
int MMBProfileID = Convert.ToInt32(Request.QueryString[0]);
}
It gives me an error
Input string was not in a correct
format.
In the aspx page, I am assigning datakeynames="Profile_ID"
How do I carry this Profile_ID to page1.
`
Thanks
Sun
Try the following:
myLink.NavigateUrl =
"~/ShowMMBProfileStats1.aspx?Profile_ID=" + e.Row.Cells[profileIDCellIndex].Text;
You're not setting the value for {0}. Try something like this:
myLink.NavigateUrl =
String.Format("~/ShowMMBProfileStats1.aspx?Profile_ID={0}", HyperLinkValue );
or
myLink.NavigateUrl = "~/ShowMMBProfileStats1.aspx?Profile_ID=" + HyperLinkValue ;

Get selected Value from dropdownlist after submit?

I populate DropDownList in ASP.NET webforms:
<asp:DropDownList runat="server" ID="salesman"></asp:DropDownList>
users= Buslayer.GetSalesRep();
foreach (userentity user in users)
{
salesman.Items.Add(new ListItem(user.FirstName + " " + user.LastName,
user.UserID.ToString()));
}
After submission, I am still getting selected index = 0,
I tried all of this but failed:
Response.Write("" + salesman.SelectedValue);
Response.Write("" + salesman.SelectedItem.Value);
Response.Write("" + salesman.SelectedIndex);
Are you checking for the Page posting back with you databind?
Your page load should look something like:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
// Get your data
ddl.DataSource = yourData;
ddl.DataBind();
}

Resources