Click here to Skip to main content
15,990,892 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, i can able to generate dynamic pages using basepage.cs,template.tmp,and like master page (createpage.ascx,headerpage.ascx,footerpage.ascx).while generating page it is dynamically generation page name like 1122348803-investment.aspx. now what i want is, i need a drop down list that must contain the dynamic pages names. should to redirect to that page once we selected that option.

to generate the dynamic pages i used this code in middle u can see the value inserting into the database.. i'm saving auto generating file name and name which is present in the drop down list into the database

C#
protected void btnGenerate_Click(object sender, EventArgs e)
   {
       String root = Server.MapPath("~");

       String pgTemplate = root + "\\myPageTemplate.tmp";

       StringBuilder line = new StringBuilder();

       using (StreamReader rwOpenTemplate = new StreamReader(pgTemplate))
       {
           while (!rwOpenTemplate.EndOfStream)
           {
               line.Append(rwOpenTemplate.ReadToEnd());
           }
       }

       int ID = 0;
       string SaveFilePath = "";
       string SaveFileName = "";
       string filename = "";
       Random ran = new Random();
       ID = ran.Next();

       //Page Name Creator
       Title = ID.ToString() + "" + txtTitle.Text.Replace(' ', '-').Replace('.', '-').Replace('#', '-').Replace('%', '-').Replace('&', '-').Replace('*', '-').Replace('@', '-').Replace('!', '-').Replace('|', '-').Replace(':', '-').Replace(';', '-').Replace(',', '-').Replace('/', '-').Replace('\\', '-').Replace('?', '-').Replace('<', '-').Replace('>', '-').Replace('"', '-').Replace('"', '-').Replace('"', '-');
       SaveFileName = "\\" + Title + ".aspx";
       filename = Title + ".aspx"; ;
       SaveFilePath = root + "\\myPages\\" + SaveFileName;
       FileStream fsSave = File.Create(SaveFilePath);

       string ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
       using (SqlConnection con = new SqlConnection(ConnectionString))
       {
           //Create the SqlCommand object
           SqlCommand cmd = new SqlCommand("spinsertvalue", con);
           //Specify that the SqlCommand is a stored procedure
           cmd.CommandType = System.Data.CommandType.StoredProcedure;

           //Add the input parameters to the command object
           cmd.Parameters.AddWithValue("@value", filename);
           cmd.Parameters.AddWithValue("@optionname", txtTitle.Text);
                      //Add the output parameter to the command object
                       //Open the connection and execute the query
           con.Open();
           cmd.ExecuteNonQuery();

           //Retrieve the value of the output parameter

       }
       if (line != null)
       {
           line.Replace("[MetaTitle]", txtTitle.Text.Replace("<", "<").Replace(">", ">").Replace('"', ' ').Replace('"', ' '));
           //line.Replace("[Content]", txtContent.Text.Replace('"', ' ').Replace('"', ' ').Replace('<' , '-').Replace('>', '-'));
           line.Replace("[ID]", ID.ToString());
           StreamWriter sw = null;

           try
           {
               sw = new StreamWriter(fsSave);
               sw.Write(line);
               lnkNewPage.Text = SaveFilePath;


           }
           catch (Exception ex)
           {
               lblMessage.Text = ex.Message;
           }
           finally
           {
               sw.Close();
           }
       }

   }


ASP.NET
 <asp:DropDownList ID="DropDownList1" DataTextField="value" 
    DataValueField="optionname" runat="server"
                AutoPostBack="True"  onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>



here i'm trying to fetch the data from database to redirect to the page when we select the option in the drop down list.. but the problem is it is not taking option name in the drop down list in string dropvalue = DropDownList1.DataValueField; always showing null
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       string dropvalue = DropDownList1.DataValueField;

       string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
       using (SqlConnection con = new SqlConnection(CS))
       {
           //Create an instance of SqlCommand class, specifying the T-SQL command
           //that we want to execute, and the connection object.
           SqlCommand cmd = new SqlCommand("spGetDropdownValue", con);
           //Specify that the SqlCommand is a stored procedure
           cmd.CommandType = System.Data.CommandType.StoredProcedure;

           //Add the input parameters to the command object
           cmd.Parameters.AddWithValue("@optionname", DropDownList1.SelectedItem.Text);
           con.Open();
           //As the T-SQL statement that we want to execute return a single value,
           //use ExecuteScalar() method of the command object.
           //Since the return type of ExecuteScalar() is object, we are type casting to int datatype
           string TotalRows = (string)cmd.ExecuteScalar();
           Response.Write("Total Rows = " + TotalRows.ToString());
           string path = "~/myPages/" + TotalRows.ToString();
           Response.Redirect(path);
       }


   }
Posted
Updated 21-Apr-15 16:47pm
v4
Comments
Deepak Kanswal Sharma 20-Apr-15 23:28pm    
You mean you need the dropdownlist containing each element as a link to seperate page?
chatarji 20-Apr-15 23:32pm    
ya.. once i choose the option, it must redirect to that page
JoCodes 21-Apr-15 1:42am    
And where are you stuck now?
upendra shahi 21-Apr-15 2:12am    
Where is issue in your code dude?

1 solution

Follow The Steps

1st set ddl dropdown list autopostback property true

2nd use its onchange Event and write follow code behind on Event`s function

C#
protected void ddlonchange(object sender,EventArgs args)
{
    if(ddl.SelectedValue="1")
    {
       Response.Redirect("A.aspx")
    }
    else if(ddl.SelectedValue="2")
    {
    Response.Redirect("B.aspx")
    }
}

This is Just an Example hope you get some idea
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900