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