ListBox and PostBackUrl event


C

Clodoaldo

I'm just starting in Asp.Net .

A page have a ListBox and I want that on the OnSelectedIndexChanged
event a second page be called passing the value of the selected
option. I want the second page to show its own url.

This is the aspx of the first page:

<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem Value="1" Text="1st Option" />
<asp:ListItem Value="2" Text="2nd Option" />
<asp:ListItem Value="3" Text="3rd Option" />
</asp:ListBox>
</div>
</form>

The problem is that the ListBox control does not have a PostBackUrl
event like the Button control has and I don't want to force the user
to choose an option and then click a button. I want it to happen at
the OnSelectedIndexChanged event and be able to use PreviousPage in
the second page. I know there are other controls that can use the
PostBackUrl event and could be used in this simple case but the number
of options can be tens or hundreds to be filled from a database and I
want it to render a select tag.

The only solution I see is to use Response.Redirect:

protected void ListBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
Response.Redirect("secondPage.aspx?option=" +
ListBox1.SelectedValue);
}

Using the redirect adds one more round trip. Beyond that what to do if
I needed to post it in instead of passing the pairs in a query
string?

I tried to change the action attribute of the form to
"secondPage.aspx" but asp.net always render it as "default.aspx".

What is the most used pattern here?

Regards, Clodoaldo Pinto Neto
 
Ad

Advertisements

T

Teemu Keiski

Hi,

Hi,

1. Specify the ListBox as

<asp:ListBox ID="ListBox1" runat="server" >
<asp:ListItem Value="1" Text="1st Option" />
<asp:ListItem Value="2" Text="2nd Option" />
<asp:ListItem Value="3" Text="3rd Option" />
</asp:ListBox>

2. Add this code in the default.aspx(.cs)

public ListBox TheListBox
{
get
{
return ListBox1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//Generate the cross-page postback script
PostBackOptions options = new PostBackOptions(ListBox1);
//This will trigger correct script generation
options.ActionUrl = "secondPage.aspx";

//Add it to onchange attribute if the ListBox
string s = Page.ClientScript.GetPostBackEventReference(options);
ListBox1.Attributes["onchange"]=s;

}

3. Then on secondPage.aspx

3.1 In aspx

<%@ PreviousPageType VirtualPath="~/Default.aspx" %>

3.2 in Page_Load of the secondPage.aspx(.cs)

protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Response.Write("You selected " +
PreviousPage.TheListBox.SelectedValue );
}
}
 
T

Teemu Keiski

I also blogged about the case
http://aspadvice.com/blogs/joteke/a...age-postback-on-ListBox-selection-change.aspx


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

Teemu Keiski said:
Hi,

Hi,

1. Specify the ListBox as

<asp:ListBox ID="ListBox1" runat="server" >
<asp:ListItem Value="1" Text="1st Option" />
<asp:ListItem Value="2" Text="2nd Option" />
<asp:ListItem Value="3" Text="3rd Option" />
</asp:ListBox>

2. Add this code in the default.aspx(.cs)

public ListBox TheListBox
{
get
{
return ListBox1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//Generate the cross-page postback script
PostBackOptions options = new PostBackOptions(ListBox1);
//This will trigger correct script generation
options.ActionUrl = "secondPage.aspx";

//Add it to onchange attribute if the ListBox
string s = Page.ClientScript.GetPostBackEventReference(options);
ListBox1.Attributes["onchange"]=s;

}

3. Then on secondPage.aspx

3.1 In aspx

<%@ PreviousPageType VirtualPath="~/Default.aspx" %>

3.2 in Page_Load of the secondPage.aspx(.cs)

protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Response.Write("You selected " +
PreviousPage.TheListBox.SelectedValue );
}
}


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

Clodoaldo said:
I'm just starting in Asp.Net .

A page have a ListBox and I want that on the OnSelectedIndexChanged
event a second page be called passing the value of the selected
option. I want the second page to show its own url.

This is the aspx of the first page:

<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem Value="1" Text="1st Option" />
<asp:ListItem Value="2" Text="2nd Option" />
<asp:ListItem Value="3" Text="3rd Option" />
</asp:ListBox>
</div>
</form>

The problem is that the ListBox control does not have a PostBackUrl
event like the Button control has and I don't want to force the user
to choose an option and then click a button. I want it to happen at
the OnSelectedIndexChanged event and be able to use PreviousPage in
the second page. I know there are other controls that can use the
PostBackUrl event and could be used in this simple case but the number
of options can be tens or hundreds to be filled from a database and I
want it to render a select tag.

The only solution I see is to use Response.Redirect:

protected void ListBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
Response.Redirect("secondPage.aspx?option=" +
ListBox1.SelectedValue);
}

Using the redirect adds one more round trip. Beyond that what to do if
I needed to post it in instead of passing the pairs in a query
string?

I tried to change the action attribute of the form to
"secondPage.aspx" but asp.net always render it as "default.aspx".

What is the most used pattern here?

Regards, Clodoaldo Pinto Neto
 
Ad

Advertisements

C

Clodoaldo

Hi,

Hi,

1. Specify the ListBox as

<asp:ListBox ID="ListBox1" runat="server" >
<asp:ListItem Value="1" Text="1st Option" />
<asp:ListItem Value="2" Text="2nd Option" />
<asp:ListItem Value="3" Text="3rd Option" />
</asp:ListBox>

2. Add this code in the default.aspx(.cs)

public ListBox TheListBox
{
get
{
return ListBox1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//Generate the cross-page postback script
PostBackOptions options = new PostBackOptions(ListBox1);
//This will trigger correct script generation
options.ActionUrl = "secondPage.aspx";

//Add it to onchange attribute if the ListBox
string s = Page.ClientScript.GetPostBackEventReference(options);
ListBox1.Attributes["onchange"]=s;

}

3. Then on secondPage.aspx

3.1 In aspx

<%@ PreviousPageType VirtualPath="~/Default.aspx" %>

3.2 in Page_Load of the secondPage.aspx(.cs)

protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Response.Write("You selected " +
PreviousPage.TheListBox.SelectedValue );
}
}

Works perfectly. Thanks.

Clodoaldo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top