Dropdownlist 返回 null 并回发部分页面

发布于 2024-12-01 02:47:23 字数 1030 浏览 2 评论 0 原文

我试图在部分页面回发后从下拉列表中读取值。由于某些原因,它始终为空。

ASP:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
       <asp:DropDownList ID="ddlW1SundayProject1" runat="server" 
            DataSourceID="dataProjectList" 
            DataTextField="ProjectName" DataValueField="Project_Id"
            AppendDataBoundItems="true" 
            onBlur="validateProjectTask('W1', 'Sunday', 1);" 
            AutoPostBack="True" ">
            <asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
        </asp:DropDownList>
        <asp:Label ID="lblW1SundayProject1" runat="server"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{

   if (ScriptManager.IsInAsyncPostBack)
   {
       lblW1SundayProject1.Text = "User selected: " + Request.Form["ddlW1SundayProject1"];   // this is always null
   }
}

输出为:“用户选择:”

I'm trying to read a value from a dropdown list after a partial page post back. For some reasons, it's always null.

ASP:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
       <asp:DropDownList ID="ddlW1SundayProject1" runat="server" 
            DataSourceID="dataProjectList" 
            DataTextField="ProjectName" DataValueField="Project_Id"
            AppendDataBoundItems="true" 
            onBlur="validateProjectTask('W1', 'Sunday', 1);" 
            AutoPostBack="True" ">
            <asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
        </asp:DropDownList>
        <asp:Label ID="lblW1SundayProject1" runat="server"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{

   if (ScriptManager.IsInAsyncPostBack)
   {
       lblW1SundayProject1.Text = "User selected: " + Request.Form["ddlW1SundayProject1"];   // this is always null
   }
}

Out put is: "User selected: "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2024-12-08 02:47:23

它不起作用,因为 ASP.NET 服务器控件(如 DropDownList)会破坏表单字段的名称/ID。因此,使用您为控件 ID (ddlW1SundayProject1) 键入的名称在 Form 集合中查找将找不到任何内容(因为 html 中的实际名称类似于 ctl00_UpdatePanel1_ddlW1SundayProject1)。

要获取回发的值,您可以使用 ddlW1SundayProject1.SelectedValue,或者如果您出于某种奇怪的原因需要查看 Form 集合,则可以执行 Request.Form[ddlW1SundayProject1.ClientID ]。

It won't work because ASP.NET server controls (like the DropDownList) mangle the names/ids of the form fields. So using the name you typed for the control ID (ddlW1SundayProject1) to look in the Form collection will not find anything (because the actual name in the html would be something like ctl00_UpdatePanel1_ddlW1SundayProject1).

To get the posted back value, you can either use ddlW1SundayProject1.SelectedValue, or if you need to look in the Form collection for some odd reason, you can do Request.Form[ddlW1SundayProject1.ClientID].

岁月静好 2024-12-08 02:47:23

如果您在 Visual Studio 中没有收到任何错误,它似乎工作正常,因为我在您的代码中看到的唯一不正确的地方是您的 标记格式不正确在 AutoPostBack="True" "> 处,您有一个额外的 "

It seems to be working fine if you are not getting any error in Visual Studio, because the only thing i can see with your code which is not correct is your <asp:DropDownList tag is not well formed at AutoPostBack="True" "> where you have an extra "

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文