Hi,
I have a page with a dropdownlist which consists of cityname from a master table.. Beside the dropdownlist i have a button,if the cityname does not exists the user can click the button beside the dropdownlist which will redirect the user to another page to add the city name.. After the user has added the cityname he should come back to the page where he was.. How to do this?
Loading
Satyapriya NayakPosted Sep 20, 2011, 8:17 AM
If this post helps you mark it as answer
Thanks
Satyapriya NayakPosted Sep 19, 2011, 8:12 AM
Here is your solution
Note:- It is better to use Server.Transfer for redirect purposes as it is faster.Generally,Response.redirect is used to redirect purpose from one domain to another domain
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
DropDownList_city.Items.Add("Choose city");
con.Open();
str = "select * from area";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList_city.Items.Add(reader["city"].ToString());
}
reader.Close();
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("Default2.aspx");
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "insert into area(city) values('" + txt_city.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Server.Transfer("Default.aspx");
}
}
Thanks
If this post helps you mark it as answer
Prabhu RajaPosted Sep 19, 2011, 7:05 AM
You Can Redirect to any page at any time By Using Redirect in Response Object.
Try this, write this code inside of your Button_Click() Event.
this.Response.Redirect("Your Page Address or name");
---------------------------------------------
If this post helps you, then mark as "Correct Answer"
Thank You