ASP.NET - 以編程方式控制下拉列表回發

[英]ASP.NET - Control dropdownlist postback programmatically


I have two dropdownlists on my form-ddl1 and ddl2. They together determine the visibility of a textbox -txt1. For that I do this check:

我的form-ddl1和ddl2上有兩個下拉列表。它們共同決定了文本框-txt1的可見性。為此我做了這個檢查:

if (ddl1.SelectedIndex==2 && ddl2.SelectedIndex>2)
   {
     if (!txt1.Visible)
        {txt1.Visible=true;// And then I want to call postback}

   }
else
   {
     if (txt1.Visible)
        {txt1.Visible=false;// And then I want to call postback}
   }

As you can see, I want to post the page to server only if the above condions are true. The code above is triggered on SelectedIndexChanged event of the both dropdownlists. How can I or is it possible to achieve upon a condition?

如您所見,我想僅在上述條件為真的情況下才將頁面發布到服務器。上述代碼在兩個下拉列表的SelectedIndexChanged事件中觸發。我怎么能或有可能達到條件?

2 个解决方案

#1


7  

I am not sure if i understand your problem but you want to achieve postback only if certain condition is met. you can hook up a javascript function on both dropdown onchange="return onchange();" Set Autopostback = true;

我不確定我是否理解你的問題,但只有在滿足某些條件時才想實現回發。你可以在下拉列表中連接一個javascript函數onchange =“return onchange();”設置Autopostback = true;

      function Onchange() {
        var ddl1 = document.getElementById('<%= ddl1.ClientID %>');
        var ddl2 = document.getElementById('<%= ddl2.ClientID %>');
        var txtbox = document.getElementById('<%= txtbox.ClientID %>');
        if (ddl1.selectedIndex == 2 && ddl2.selectedIndex > 2) {
            txtbox.style.display = "inline";
            __doPostBack(ddl1, '');
        }
        else {
            txtbox.style.display = "none";
            return false;
        }
    }

Aspx code should look like this.

Aspx代碼應如下所示。

<asp:DropDownList runat="server" AutoPostBack="true" ID="ddl1" onchange="return Onchange();"
            OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
            <asp:ListItem Text="text1" />
            <asp:ListItem Text="text2" />
            <asp:ListItem Text="text3" />
            <asp:ListItem Text="text4" />
        </asp:DropDownList>
        <asp:DropDownList runat="server" AutoPostBack="true" ID="ddl2" onchange="return Onchange();"
            OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
            <asp:ListItem Text="text1" />
            <asp:ListItem Text="text2" />
            <asp:ListItem Text="text3" />
            <asp:ListItem Text="text4" />
        </asp:DropDownList>
        <asp:TextBox runat="server" ID="txtbox" />

Tested it and it works...

測試它,它的工作原理......

#2


1  

If AutoPostBack = True, which it would have to be for your events to be firing just call a funciton when your condition is met. ASP.NET is always posting back, you just need to handle the condition, otherwise you have to handle the validation with JavaScript and manually post the page:

如果AutoPostBack = True,那么你的事件必須被觸發才會在你的條件滿足時調用一個函數。 ASP.NET總是回發,你只需要處理這個條件,否則你必須用JavaScript處理驗證並手動發布頁面:

   if (ddl1.SelectedIndex==2 && ddl2.SelectedIndex>2)
   {
        if (!txt1.Visible)
        {
           txt1.Visible=true;// And then I want to call postback
           //dowork
        }

   }
   else
   {
        if (txt1.Visible)
        {
            txt1.Visible=false;// And then I want to call postback
            //do work
        }
   }

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/05/02/7200099f95a4c06c650168bda362f77d.html



ASP.net下拉列表回發dosnot在jquery和控制面板中工作 - ASP.net dropdown list postback dosnot work inside jquery and control panel 是否有可能獲得回發效果(如在ASP中)。沒有javascript的下拉列表? - Is it possible to get postback effect (like in ASP.NET Webform) with dropdownlist without javascript? asp.net如何以編程方式為大約10個下拉列表添加TemplateField - asp.net how to add TemplateField programmatically for about 10 dropdownlist 如何以編程方式設置ASP。NET Web Forms下拉列表selecteditem和selectedindex - How to programmatically set a ASP.NET Web Forms dropdownlist selecteditem and selectedindex 處理級聯下拉列表的最佳實踐ASP.NET MVC中的其他回發方案 - Best Practice for handling cascading dropdowns other postback scenarios in ASP.NET MVC 從ASP.NET下拉列表中使用修改后的查詢字符串進行回發 - Postback with Modified Query String from Dropdown in ASP.NET 下拉列表中的ListItems屬性在回發時丟失了嗎? - ListItems attributes in a DropDownList are lost on postback? MVC 4下拉列表更改的回發 - MVC 4 postback on Dropdownlist change 在獲取下拉列表值c# asp時避免回發 - Avoid postback when getting dropdownlist value c# asp 使用jquery啟用asp.net下拉列表控件 - enable asp.net DropDownList control using jquery
 
粤ICP备14056181号  © 2014-2020 ITdaan.com