First post for me, hope this is right place rather than c#

I have a form. When it is completed, it redirects the user to a thank you page.
<%@ Page Language="C#" masterpagefile="../../../msrtpage/hra2.master" title="Sign Up Here" %>
<asp:Content id="Content1" runat="Server" contentplaceholderid="hra1">
<script runat="server">
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
Response.Redirect("thanksfirst.aspx");
}
<asp:FormView id="FormView1" runat="server" CssClass="dform" DataKeyNames="ID" DataSourceID="entrysheet" DefaultMode="Insert" OnItemInserted="FormView1_ItemInserted" >

I left the full form out of this but the button command is "insert".

In my example, these are my listitem values bound to a db column "event"
<asp:listbox id="ListBox5" runat="server" selectedValue='<%# Bind("event") %>' Rows="3">
<asp:listitem Value="Falcon"></asp:listitem>
<asp:listitem Value="Fairlane"></asp:listitem>
<asp:listitem Value="Galaxie"></asp:listitem>
</asp:listbox>

So what I need help with is to define the response.redirect so that the user goes to the correct page based on the selected item. For example, if "Falcon" is selected, it redirects to thanksFalcon.aspx, if Fairlane is selected, redirects to thanksFairlane.aspx, "Galaxie" redirects to thanksGalaxie.aspx.

The closest I've come to solving this, with some help, is

<script runat="server">

protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
Control myControl = FindControl("ListBox5");
string urlpart = ((ListBox) myControl).SelectedItem.Text;
HttpContext.Current.Response.Redirect("thanks" + urlpart + ".aspx");
}
</script>

This renders a page but produces this error when "insert" is pressed:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Most all the other attempts produce an error before the page renders similar to:

CS0103: The name 'ListBox5' does not exist in the current context

I'd sincerely appreciate any help to solvet his problem. My skills with c# are rudimentary.

The listbox control 'ListBox5' is inside the FormView control.
You should find control in the FormView control not the Page.

So change your code like below...

<script runat="server">

protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
        ListBox myControl = (ListBox)FormView1.FindControl("ListBox5");
        string urlpart =  myControl.SelectedItem.Text;
        HttpContext.Current.Response.Redirect("thanks" + urlpart + ".aspx");

}
</script>

So far in testing this is perfect....thank you very much.

Hi daytonasteve,

Mark this thread as solved if your question is answered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.