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
        }
   }
智能推荐

注意!

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



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告