I’m having a little problem in my web app development. On my masterpage I have a DropDownList. Let’s call it MyDDL. When I first load my app (after login) I populate it with some data from SQL database table, let’s call it MyTable. No problem there. On my masterpage I also have a menu that redirects user to different pages (content) of my app. When user first selects a value from MyDDL I need it to remain no matter on wich page user clicks (because data on that page is dependand on value selected in MyDDL). Problem I have is, value in MyDDL is reset to “default” (first value in MyTable) everytime I load different page. Is there a way to solve this? Any help is appritiated.
Here is my code behind the masterpage:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM MyTable";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
try
{
MyDDL.DataSource = cmd.ExecuteReader();
MyDDL.DataTextField = "izdelek";
MyDDL.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.StackTrace);
}
}
}
There are many ways to solve this problem.
But i suggest you to used session Or querystring also check this link for querystring querystring msdn.
OnSelectedIndexChanged of dropdownlist save value in session or pass it to webpage using querystring.
On masterpage as you give datasource to dropdownlist and then bind it after that use:
Hope it helps to solve your problem.