Get POST Values in ASP.NET using NameValueCollection

会有一股神秘感。 提交于 2019-12-11 14:11:37

问题


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.


回答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"];


来源:https://stackoverflow.com/questions/12011905/get-post-values-in-asp-net-using-namevaluecollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!