1

如何在没有自动回发的情况下在标签中获取下拉列表选择的值,并在 asp.net 中更新面板。我想要此代码的客户端脚本我有以下代码:-

protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
    Label1.Text = DropDownList1.SelectedValue;
   // DropDownList1.Attributes["onclick"] =
   //"Label1.Text=this.options[this.selectedIndex].value";
}
4

4 回答 4

3

如果您不想使用 jquery(不是每个人都这样做!:)),您可以使用标准 javascript

<script language="javascript" type="text/javascript">
    function setLabelText() {
        var dropdown = document.getElementById("DropDownList1");
        document.getElementById("Label1").innerHTML = dropdown.options[dropdown.selectedIndex].text;
    }

</script>
<asp:DropDownList ID="DropDownList1" ClientIDMode="Static" runat="server" AutoPostBack="false" onchange="setLabelText();">
  <asp:ListItem Value="1" Text="One" />
  <asp:ListItem Value="2" Text="Two" />
</asp:DropDownList>

<asp:Label ID="Label1" runat="server" Text="Label" ClientIDMode="Static"></asp:Label>
于 2012-08-17T13:30:46.553 回答
2

在您的 CS 代码中,添加一个属性,例如:

ddlMyDrop.attributes.add("onchange","SetLabel(this,lblCtrl)");

在您的 JS 代码中...

function SetLabel(sender, target){
    $(target).val($(sender).val());
}

这假设您引用 jQuery。

于 2012-08-17T13:03:59.787 回答
1

You can do this fairly easily with jQuery. Label's turn into span and DropDownList into select on client side. Keep in mind that asp.net loves to append strings to the resultant content id's, e.g. MainContent_...

$(document).ready(function () {
    $('#MainContent_DropDownList1').change(function () {
        try {
            $('#MainContent_Label1').text($(this + "option:selected").text());
        } catch (err) {
            alert(err);
        }
    });
});
于 2012-08-17T13:20:02.620 回答
0

不使用 jQuery 或 Javascript,因为这是对现有站点的修复,并且不是那样设计的。Well I have gotten to a point where when the DropDownList is selected and does it's postBack I do my logic of setting the textBox readOnly status to true or false. 我现在遇到的问题是 selectValue 不一致。它在 selct 字段中显示的内容不是发布回页面的内容。假设我有 None, 5.00, 10.00, 15.00, 20.00 作为我的选择。我首先选择 10.00 并返回 None 然后我选择 20.00 它显示 10.00。它回发先前的选择值。整个网站是从页面背后的代码编写的。aspx 页面完全是从 .vb 页面写入的。一切都写入asp标签。这是代码;

            If Page.IsPostBack Then
                If product_option_is_required > 0 then
                    myTextBox.ReadOnly= true
                Else
                    myTextBox.ReadOnly= false
                End if
                For Each child_control As Control In productOptions.Controls
                    If TypeOf child_control Is DropDownList Then
                        Dim child_ddl As DropDownList = child_control
                        tempName = products.getProductDependant("product_option_name",product_option_id)
                        tempSelectText = products.getProductSelectDependant("product_option_detail_name",child_ddl.SelectedValue)
                        priceDependant.Text ="here" & child_ddl.ID & " " & child_ddl.SelectedIndex & " " & child_ddl.SelectedValue & " --" & tempSelectText
                        If child_ddl.Text = "None" then
                            myTextBox.ReadOnly = true
                            myTextBox.Text = "If selected above enter name"
                        Else
                            myTextBox.ReadOnly = false
                            myTextBox.Text = ""
                        End if
                    End If

                next
            End if
于 2013-01-25T23:42:58.870 回答