0

So I have 2 pages. They both have the same master page and are part of the same Web Application. I am trying to submit one page to the other. On the submitting page I have a few elements such as

<ajaxToolkit:ComboBox ID="cmboOptions" AutoCompleteMode="SuggestAppend"
CaseSensitive="false" ItemInsertLocation="Append" runat="server" DropDownStyle="DropDownList">
    <asp:ListItem></asp:ListItem>
    <asp:ListItem Text="Option 1" Value="opt1"></asp:ListItem>
    <asp:ListItem Text="Option 2" Value="opt2"></asp:ListItem>
</ajaxToolkit:ComboBox>

I am using

<asp:Button ID="btnSubmit" runat="server" Text="Submit" 
PostBackUrl="~/Results.aspx" />

to submit the page.

On the results page in the code behind on page load I have

NameValueCollection nvc = Request.Form;
string selectedOption = nvc["cmboOptions"];

If I look in the NVC on debug I can see

ctl00$MainContent$cmboOptions$TextBox

with a value of "Option 1" yet my string still contains a value of null. I do not want to even bother trying to get the value by hard coding the obfuscated ID's in and I can't set a name property on ASP.net elements.

Does anyone know a better/proper way to accomplish what I am doing? I would like to stick with the ajaxControlToolkit comboboxes because they are nice for the user although I wish I stuck with jQuery instead of listening to my friend and now it's too late to switch.

I looked here without any luck

Get POST data in C#/ASP.NET

and here

Read Post Data submitted to ASP.Net Form

and here

http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx

and I tried PreviousPage.FindControl and I always get null.

Thanks for your time!

Edit:

Avoid the AjaxControlToolKit. It is nice if you want to be lazy and drag->drop sweet UI elements but is just a headache to get simple things working! Use jQuery.

Community
  • 1
  • 1
Steve
  • 652
  • 3
  • 11
  • 23
  • 1
    your code seems to ok, problem may be caused by ajax combo box, Try any asp control to check you code. Also try Previous Page as I mentioned in my answer. – Waqar Janjua Aug 17 '12 at 19:47

1 Answers1

1

I think you have to use Page.PreviousPage Property You can get the dropdown value as

if (Page.PreviousPage != null)
{
   DropDownList ddl= (DropDownList)Page.PreviousPage.FindControl("cmboOptions$cmboOptions_TextBox");
   // You have an AjaxToolkit Combo Box, so you must cast it as
   AjaxToolKit.ComboBox ddl= (AjaxToolKit.ComboBox )Page.PreviousPage.FindControl("cmboOptions"); 
   if (ddl != null)
   {
      // do your work
   }
}

Read more on msdn about Cross Page Posting in Asp.net

Updated Answer: To check the Steve code I created a page with a dropdownlist and a button to do postback ( Currently I don't have ajaxtoolkit so I'm using dropdownlist )

<asp:DropDownList ID="ddl" runat="server"  >
    <asp:ListItem></asp:ListItem>
      <asp:ListItem Text="Option 1" Value="opt1"  > </asp:ListItem>
      <asp:ListItem Text="Option 2" Value="opt2"  ></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" 
    PostBackUrl="~/Default4.aspx" />
 // This code was written on Default5.aspx

 // Default4.aspx code behind
protected void Page_Load(object sender, EventArgs e)
{
    NameValueCollection nvc = Request.Form;
    string val = Request.Form["ddl"];
    string val2 = nvc["ddl"];
    // Both above statement returns the required result
}

So, i think the problem is with the Ajax Combo box.( if you are not doing any mistake )

Updated Answer: The problem is that the ID of Ajax Combo box control is changed when we post the page. In the Next page we can get the ID of ajax combox box as

For Pages Without Master Pages: If you are using an aspx page without master page then you can get the ID of ajax combo box as

     //Ajax Combo Box ID format
     ComboBoxID + "$TextBox" 
     // so If I have a combo box with ID ComboBox1 it becomes
     ComboBox1$TextBox
     so we will get the value as
     string comboBoxvalue = Request.Form["ComboBox1$TextBox"];
                   or
     NameValueCollection nvc = Request.Form;
     string cmbvalue = nvc["ComboBox1$TextBox"];

For Pages using MasterPages:

    //Ajax Combo Box ID format
    "ctl00$" + ContentPlaceHolderID +"$" + ComboBoxID + "$TextBox"
    //I have a combox Box with ID ComboBox1 and ContentPlaceHolderID ContentPlaceHolder1
    so AjaxComboBox ID becomes ctl00$ContentPlaceHolder1$ComboBox1$TextBox

    string cmbvalue = nvc["ctl00$ContentPlaceHolder1$ComboBox1$TextBox"];

    // In your case
   // ComboxBox ID is cmboOptions  and ContentPlaceHolderID is MainContent
    // so your ID becomes
    ctl00$MainContent$cmboOptions$TextBox
    // so you will get the data as
     string cmbvalue = nvc[" ctl00$MainContent$cmboOptions$TextBox"];
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
  • I already tried this one. No luck with it :(. That link is almost identical to the one I posted too and I tried all the things on there with no luck :( – Steve Aug 17 '12 at 20:25
  • 1
    any error ? When you press the button then you had selected an item in the combobox ? – Waqar Janjua Aug 17 '12 at 20:27
  • 1
    @Steve please replace ajaxToolkit:ComboBox with dropdownlist and check your code, if your code works it means problem is with ajax ComboBox. I checked your code it works fine for asp dropdownlist, so the possible problem is ajax combo box. plz confirm it. Thanks – Waqar Janjua Aug 17 '12 at 20:51
  • Alright I will try and get to it within the next day or two and post the results. – Steve Aug 17 '12 at 21:28
  • I can indeed verify that this works exactly as expected. Will the ct100$ always be the same though? One of the things I want to avoid is an issue down the road because that generated value changes and I have it hard coded in. Also, thanks for putting so much work into this answer! That is dedication to helping! – Steve Aug 20 '12 at 14:31
  • Alright. Fantastic. Thanks for all your help. Hopefully this page helps others too! – Steve Aug 20 '12 at 14:37