Developer forum

Forum » Development » RE: Response.Redirect() not working within a User Control.

RE: Response.Redirect() not working within a User Control.

Yasharth Tahiliani
Reply

Hi,

I am facing problems while trying to Redirect to another page whilst clicking a button that is within a user control on a page.

    protected void btn_Click(object sender, EventArgs e)
    {
        System.Web.HttpContext.Current.Response.Redirect("CurrentVacancies.aspx?department=" + departmentDD.SelectedValue.ToString() + "&location=" +                   locationDD.SelectedValue.ToString() + "&type=" + typeDD.SelectedValue.ToString() + "&keyword" + keyword.Text);
    }

Can you please guide me on how to solve this?

Regards,

Yash.

 


Replies

 
Nicolai Høeg Pedersen
Reply

If you post the error we might be able to...

 
Yasharth Tahiliani
Reply

It's actually just refreshing the same page, and not redirecting it anywhere..

 
Nicolai Høeg Pedersen
Reply

Hi Yasharth

ok. But if you do not describe what the error is, or what happens or what does not happen, it is not easy to help figure out what can be wrong...

You write "Im facing problems" - but what problems?

Nicolai

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Are you sure your method is being called? Have you tried debugging and see if your code actually executes?

Imar

 
Yasharth Tahiliani
Reply

So I have a page that uses a Razor template which has a User Control in it. The user control has 3 dropdowns and one text box, and one button which redirects the current page to another one along with the user input from the dropdowns and text box as query strings, as you can see.

I am not facing any errors when I click on the submit button. It should redirect the current page to the new one, but it just refreshes the current page.

I am attaching you copies of the template that the page uses, along with the acsx and code behind files of the User Control. I can even give you access to the CMS and the website if you like. 

 
Yasharth Tahiliani
Reply

Hi Imar,

I have created these User Controls locally in a website solution as outlined in your post about creating User Controls. When I run it locally, it does do that, I have debugged through the code and it does execute properly. However, within a dynamic web website on the development server, it is doing nothing, and just refreshing the page.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

>> When I run it locally, it does do that,

But that doesn't necessarily mean it also does it on the server ;-) I would confirm it runs there first...

Imar

 
Yasharth Tahiliani
Reply

Ok, I'll test that and get back to you :)

 
Yasharth Tahiliani
Reply

When I copy the local website that I have used to build the User Controls and run it as a seperate site on the server, it works. However within the DW solution it isn't working :( 

 
Nicolai Høeg Pedersen
Reply

When you have a Razor template already, why use a control?

Just implement the form in Razor/html - a lot more modern and future proof...

 
Yasharth Tahiliani
Reply

I could do that yes, but the dropdowns in the form get their data from a database. 

I don't have experience of doing that in Razor. If you could point me in the right direction, then that would be really appreciated.

Also, this is baffling me. I just put a label on the form page and changed it's text on button click event. Nothing happens. It seems that within DW, the button is failing to register the OnClick event for some reason :(

 
Nicolai Høeg Pedersen
Reply

Razor and controls are both C# or .NET code. It is the exact same thing... Razor is just only 5 years old, and controls 15...

Take a look at the Razor introduction here: http://developer.dynamicweb.com/documentation/for-designers.aspx

And create a datareader loop around a list of options:

using (var myDr = Database.CreateDataReader("SELECT TOP 10 * FROM Page"))
           {
                while (myDr.Read())
                {
                    <option value="@myDr["PageID"]">@myDr["PageMenuText"]</option>
                }
            }

 
Yasharth Tahiliani
Reply

Ok thanks, I'll see if I can implement this. However, I would really like to request you to please investigate into why we cannot achieve the simplest of things using User Controls. We chose DynamicWeb as a CMS when we could have chosen other CMS such as Kentico which we have worked with for ages, and have never ran into any problems.

Why don't you implement the User Control that I sent over in one of your sample websites? Just try implementing a button click event and see if it works. Please get back to me.

Thanks!

 
Yasharth Tahiliani
Reply

I just realised that when I access the page by clicking Show Page from within the CMS, it opens the page with a different URL. In this case, the URL is 

http://nstwebdev.int.rroom.net/Default.aspx?ID=72&Purge=True

This actually works!

However, when the page is accessed using the true URL, which it will be when the site is published live, it doesn't work! The real URL of the page is:

http://nstwebdev.int.rroom.net/en-GB/Careers.aspx

The User Control doesn't seem to be working properly when accessed from that URL.

This might help you investigate the problem better. Can you please look into this and get back to me?

Regards,

Yash.

 
Nicolai Høeg Pedersen
Reply

Hi Yasharth

Using this control (removed your codebehind because I do not have the database):

Search for jobs
<form id="CareersForm" runat="server">
    <asp:PlaceHolder ID="careersSearchSection" runat="server">
     <div class="career-filter side-widget">
        <h3>Search for jobs</h3>
        <br />
        <div class="form-input select">
            <div class="field">
                <asp:DropDownList ID="departmentDD" runat="server">
                     <asp:ListItem Text="Department" Value=""/>
                </asp:DropDownList>
            </div>
        </div>
        <div class="form-input select">
            <div class="field">
                <asp:DropDownList ID="locationDD" runat="server">
                     <asp:ListItem Text="Location" Value=""/>
                </asp:DropDownList>
            </div>
        </div>
        <div class="form-input select">
            <div class="field">
                <asp:DropDownList ID="typeDD" runat="server">
                     <asp:ListItem Text="Type" Value=""/>
                </asp:DropDownList>
            </div>
        </div>
        <div class="form-input">
            <asp:TextBox ID="keyword" runat="server" />
        </div>
        <asp:Button runat="server" ID="goBtn" OnClick="btn_Click" Text="Yash" />
     </div>
    </asp:PlaceHolder>
</form>
<script language="CS" runat="server">
    void Page_Load(object sender, System.EventArgs e)
    {
      // Put user code to initialize the page here
    }
    void btn_Click(object sender, System.EventArgs e)
    {
     goBtn.Text = "Nicolai";
    }
  </script>

Gives me frist screen 1 (See attachment) and then clicking the button gives me screen 2 (see attachment).

So it does work.

My guess is, that since you use a codebehind, you need to compile your project, and make sure the dll is in the solutions /Bin folder.... So the code is not run in your production because you have not moved it out there?

BR Nicolai

Capture.PNG Capture2.PNG
 
Yasharth Tahiliani
Reply

Hi Nicolai,

Thanks for trying it out. 

The project is a WebSite solution so it doesn't have dlls. I did the same thing as you and implemented the OnClick in the ascx within a <script> element. 

However, it isn't working when using friendly URL of the page. It does work when you use the custom dynamic web URL with the ?id attached in the end, as in  your case when it did work.

That seems to be the root of the problem. If you could please investigate it further, that would be really helpful.

Thanks and Regards,

Yash.

 
Yasharth Tahiliani
Reply

Guys, did you manage to investigate this issue?

We have to finish the site by Tuesday next week so it would be great if you can give us a solution asap.

As I mentioned earlier, the User Controls are working fine if you access the page using the dynamic web generated URLs but they don't if you access them using the friendly URLs. 

 
Nicolai Høeg Pedersen

 

You must be logged in to post in the forum