获取POST值在ASP.NET中使用的NameValueCollection [英] Get POST Values in ASP.NET using NameValueCollection

查看:2262
本文介绍了获取POST值在ASP.NET中使用的NameValueCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有2个页面。它们都具有相同的母版页是相同的Web应用程序的一部分。我想提出一个网页到另一个。在提交页面我有几个元素,比如

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>

我使用

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

提交页面。

在在code中的结果页面上的落后页面加载我有

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

与选项1,但我的字符串仍然包含null值的值。我不想甚至懒得想通过硬编码在混淆的ID来获取值,我不能设置ASP.net元素的name属性。

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.

有谁知道完成我在做什么更好的/正确的方法?我愿与ajaxControlToolkit组合框坚持,因为他们是很好的用户,虽然我希望我坚持,而不是听我的朋友与jQuery,现在为时已晚切换。

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

获得C#/ ASP.NET POST数据

在这里

读取数据后提交给ASP.Net表格

在这里

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

和我试过previousPage.FindControl和我总是空。

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

感谢您的时间!

编辑:

避免AjaxControlToolKit。这是很好的,如果你想偷懒,拖拽>降甜UI元素,但仅仅是一个头痛得到一些简单的工作!使用jQuery。

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.

推荐答案

我认为你必须使用的页。previousPage 物业
你可以在下拉值

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
   }
}

了解更多关于MSDN了解跨页投递在Asp.net

更新答案:
要检查史蒂夫code我创建了一个DropDownList和一个按钮做回发一个页面(目前我没有ajaxtoolkit所以我使用的DropDownList)

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 )

更新答案:
问题是,当我们发布页面阿贾克斯组合框控件的ID被改变。在接下来的页面中,我们可以得到阿贾克斯COMBOX框的ID为

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

对于没有母版页页次:
如果您使用的是aspx页面没有母版页那么你可以得到阿贾克斯组合框的ID为

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

的页面使用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"];

这篇关于获取POST值在ASP.NET中使用的NameValueCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆