我如何获得用户的DropDownList选择值Asp.Net(selectedIndexChange) [英] How do I get DropDownList selected value from user in Asp.Net(selectedIndexChange)

查看:118
本文介绍了我如何获得用户的DropDownList选择值Asp.Net(selectedIndexChange)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何去使用的SelectedIndexChanged一个更新面板中asp.Net DropDownList中获取所选值?我试过,但是当我添加的AutoPostBack我的DropDownList,它把我送到一个错误页说,它不能找到的资源。(甚至不打我的selected_IndexChanged),我有hiddenField被分配选定的值。

下面是我的DropDownList:

 < ASP:的ScriptManager ID =ScriptManager1=服务器/>
    < ASP:的UpdatePanel =服务器ID =UpdatePanel1>
        <&的ContentTemplate GT;
            < ASP:DropDownList的=服务器ID =ddlCaseFilesNew的DataSourceID =dsCaseFiles
                DataTextField =显示DataValueField =写到FileID在preRender =ddl_ preRenderWIDTH =300像素
                的AutoPostBack =真OnSelectedIndexChanged =ddlCaseFilesNew_SelectedIndexChanged可见=假>
                < ASP:ListItem的>项目1 LT; / ASP:ListItem的>
            < / ASP:DropDownList的>
        < /&的ContentTemplate GT;
        <&触发器GT;
            < ASP:AsyncPostBackTrigger控件ID =ddlCaseFilesNew事件名称=的SelectedIndexChanged/>
        < /触发器>
    < / ASP:的UpdatePanel>

当我点击DropDownList的里面把我送到一个找不到资源页的值。我不知道为什么。它甚至没有碰我onSelectedIndexChanged

 <脚本=服务器>
    保护无效ddlCaseFilesNew_SelectedIndexChanged(对象发件人,EventArgs的发送)
    {
        hidNewCaseFile.Value = ddlCaseFilesNew.SelectedItem.Value;
    }
< / SCRIPT>


解决方案

在您的SelectedIndex功能,使用

  S值的字符串=((DropDownList的)寄件人).SelectedValue;

因为它是在一个更新面板,你可能不会有通过设计师(this.DdlId)访问它。

根据在OP您的意见,这很可能是你所需要的:

 保护无效ddlCaseFilesNew_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    hidNewCaseFile.Value =((DropDownList的)寄件人).SelectedValue;
}

您还需要你的隐藏字段移动到的ContentTemplate。

How do I go about getting the selected value from the DropDownList in asp.Net using selectedIndexChanged with an update Panel? I've tried but when I add the AutoPostBack to my DropDownList, it sends me to an error page saying it cannot find the resource.(doesn't even hit my "selected_IndexChanged") I have the hiddenField being assigned the selected value.

Here is my DropDownList:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel runat="server" ID="UpdatePanel1">
        <ContentTemplate>
            <asp:DropDownList runat="server" ID="ddlCaseFilesNew" DataSourceID="dsCaseFiles"
                DataTextField="Display" DataValueField="FileID" OnPreRender="ddl_PreRender" Width="300px"
                AutoPostBack="true" OnSelectedIndexChanged="ddlCaseFilesNew_SelectedIndexChanged" Visible="False">
                <asp:ListItem>Item 1</asp:ListItem>
            </asp:DropDownList>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddlCaseFilesNew" EventName="SelectedIndexChanged" />
        </Triggers>
    </asp:UpdatePanel>

When I click on a value inside the DropDownList it sends me to a "cannot find resource" page. I have no idea why. It doesn't even touch my "onSelectedIndexChanged"

  <script runat="server">
    protected void ddlCaseFilesNew_SelectedIndexChanged(object sender, EventArgs e)
    {
        hidNewCaseFile.Value = ddlCaseFilesNew.SelectedItem.Value;
    }
</script>

解决方案

In your SelectedIndex function, use

string sValue = ((DropDownList)sender).SelectedValue;

Since it's in an update panel, you likely won't have access to it via the designer (this.DdlId).

Based on your comment in the OP, this is likely what you need:

protected void ddlCaseFilesNew_SelectedIndexChanged(object sender, EventArgs e)
{
    hidNewCaseFile.Value = ((DropDownList)sender).SelectedValue;
}

You also need to move your hidden field into the ContentTemplate.

这篇关于我如何获得用户的DropDownList选择值Asp.Net(selectedIndexChange)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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