如何通过下拉列表重定向到不同的页面 [英] how to redirect to different pages through drop down list

查看:100
本文介绍了如何通过下拉列表重定向到不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用basepage.cs,template.tmp和类似母版页(createpage.ascx,headerpage.ascx,footerpage.ascx)生成动态页面。生成页面时动态生成页面名称如1122348803 -investment.aspx。现在我想要的是,我需要一个必须包含动态页面名称的下拉列表。一旦我们选择了该选项,就应该重定向到该页面。



生成动态页面我在中间使用此代码你可以看到插入数据库的值..我将自动生成文件名和名称保存在数据库中下拉列表中



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

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:DropDownList ID="DropDownList1" DataTextField="value" 

    DataValueField="optionname" runat="server"

                AutoPostBack="True"  onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>







这里我试图从数据库中获取数据以重定向到当我们在下拉列表中选择选项时页面..但问题是它没有在字符串dropvalue = DropDownList1.DataValueField的下拉列表中选择选项名称;始终显示null




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

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


   }

推荐答案

按照步骤< br $>


第一套ddl下拉列表autopostback属性true



第二次使用其onchange事件并编写后面的代码事件的功能



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

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



这是Just一个例子希望你有所了解


This is Just an Example hope you get some idea


这篇关于如何通过下拉列表重定向到不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆