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

これは、jQueryを使用してかなり簡単に行うことができます。クライアント側でラベルがにspanなり、DropDownListがになります。selectasp.netは、結果のコンテンツIDに文字列を追加するのが大好きです(例: 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 を使用していません。さて、DropDownList が選択されて postBack になったときに、textBox の readOnly ステータスを true または false に設定するロジックを実行するところまで来ました。私が今抱えている問題は、selectValue が一貫していないことです。選択フィールドに表示されるものは、ページに戻されるものではありません。選択する選択肢として、なし、5.00、10.00、15.00、20.00 があるとします。最初に 10.00 を選択すると、[なし] が返され、次に 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 に答える