I want a DropDownList that passes a parameter to a new page when an item is selected. The problem is that the variable is not passing to the stored procedure.
Can anyone help please?
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WidthList.ascx.cs" Inherits="User_Control_WidthList" %>
// build an absolute URL private static string BuildAbsolute(string relativeUri) { // get current uri Uri uri = HttpContext.Current.Request.Url; // build absolute path string app = HttpContext.Current.Request.ApplicationPath; if (!app.EndsWith("/")) app += "/"; relativeUri = relativeUri.TrimStart('/'); // return the absolute path return HttpUtility.UrlPathEncode( String.Format("http://{0}:{1}{2}{3}", uri.Host, uri.Port, app, relativeUri)); }
// generate a width URL public static string ToWidth(string widthId) { return BuildAbsolute(String.Format("WidthAdmin.aspx?Width={0}", widthId)); } }
public class CatalogAccess { public CatalogAccess() {
}
// retrive list of width public static DataTable GetWidth(string id) { // get a configured dbCommand object DbCommand comm = GenericDataAccess.CreateCommand(); // set the procedure name comm.CommandText = "GetWidth"; // create a new parameter DbParameter param = comm.CreateParameter(); param.ParameterName = "@WidthID"; param.Value = id; param.DbType = DbType.Int32; comm.Parameters.Add(param); return GenericDataAccess.ExecuteSelectCommand(comm); }
I want a DropDownList that passes a parameter to a new page when an item is selected. The problem is that the variable is not passing to the stored procedure.
Can anyone help please?
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WidthList.ascx.cs"
Inherits="User_Control_WidthList" %>
<asp:FormView ID="form" runat="server" Width="120px" DataSourceID="SqlDataSource1" DataKeyNames="WidthID">
<HeaderTemplate>
Select Width:
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="Width"
DataValueField="WidthID"
SelectedValue='<%# Eval("WidthID") %>'
NavigateUrl='<%# Link.ToWidth(Eval("WidthID").ToString())%>'
Width="100px" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TyreConnection %>"
SelectCommand="SELECT [WidthID], [Width] FROM [Width]">
</asp:SqlDataSource>
public partial class User_Control_WidthList : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
//string widthId=
// Server.UrlEncode(DropDownList1.SelectedItem.Value);
//Response.Redirect(Link.ToWidth(widthId));
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//string widthId =
// Server.UrlEncode(form.SelectedValue.ToString());
//Response.Redirect(Link.ToWidth(widthId));
//string widthId = form.SelectedValue.ToString();
string widthId = Convert.ToString(form.SelectedValue);
Response.Redirect(Link.ToWidth(widthId));
}
}
public class Link
{
public Link()
{
}
// build an absolute URL
private static string BuildAbsolute(string relativeUri)
{
// get current uri
Uri uri = HttpContext.Current.Request.Url;
// build absolute path
string app = HttpContext.Current.Request.ApplicationPath;
if (!app.EndsWith("/")) app += "/";
relativeUri = relativeUri.TrimStart('/');
// return the absolute path
return HttpUtility.UrlPathEncode(
String.Format("http://{0}:{1}{2}{3}",
uri.Host, uri.Port, app, relativeUri));
}
// generate a width URL
public static string ToWidth(string widthId)
{
return BuildAbsolute(String.Format("WidthAdmin.aspx?Width={0}", widthId));
}
}
public class CatalogAccess
{
public CatalogAccess()
{
}
// retrive list of width
public static DataTable GetWidth(string id)
{
// get a configured dbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// set the procedure name
comm.CommandText = "GetWidth";
// create a new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@WidthID";
param.Value = id;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
return GenericDataAccess.ExecuteSelectCommand(comm);
}