Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / November 2007

Tip: Looking for answers? Try searching our database.

ListBox and PostBackUrl event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Clodoaldo - 10 Nov 2007 15:13 GMT
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
Teemu Keiski - 11 Nov 2007 07:51 GMT
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   );
       }
   }

Signature

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

> I'm just starting in Asp.Net .
>
[quoted text clipped - 43 lines]
>
> Regards, Clodoaldo Pinto Neto
Teemu Keiski - 11 Nov 2007 08:05 GMT
I also blogged about the case
http://aspadvice.com/blogs/joteke/archive/2007/11/11/ASP.NET_3A00_-trigger-cross
_2D00_page-postback-on-ListBox-selection-change.aspx


Signature

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

> Hi,
>
[quoted text clipped - 94 lines]
>>
>> Regards, Clodoaldo Pinto Neto
Clodoaldo - 11 Nov 2007 09:44 GMT
> Hi,
>
[quoted text clipped - 46 lines]
>         }
>     }

Works perfectly. Thanks.

Clodoaldo

> --
> Teemu Keiski
[quoted text clipped - 47 lines]
>
> > Regards, Clodoaldo Pinto Neto

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.