简体   繁体   English

如何基于ASP.net Web表单中的下拉列表选择来更改按钮的颜色?

[英]How to change the color of a button based on drop down list selection in ASP.net web forms?

I have a drop down list with a few list items inside it and I have a button and I want to change the color of that button based on the drop down list selection. 我有一个带有一些列表项的下拉列表,并且有一个按钮,我想根据下拉列表选择更改该按钮的颜色。 Every selection will give a different color to the button. 每个选择都会为按钮赋予不同的颜色。

I have this selectedindexchange event but is not working: 我有这个selectedindexchange事件,但无法正常工作:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.Text == "jim.rumdner@gmail.com")
    {
        Button1.BackColor = System.Drawing.Color.Blue;
    }
    else if (DropDownList1.SelectedItem.Text == "alon_gk@netvision.net.il")
    {
        Button1.BackColor = System.Drawing.Color.Green;
    }
    else if (DropDownList1.SelectedValue == "ohad_jl@internet-zahav.co.il")
    {
        Button1.BackColor = System.Drawing.Color.Red;
    }
    else if (DropDownList1.SelectedValue == "nirho_fg@walla.com")
    {
        Button1.BackColor = System.Drawing.Color.Yellow;
    }
    else if (DropDownList1.SelectedValue == "yidhj_ry@yahoo.com")
    {
        Button1.BackColor = System.Drawing.Color.White;
    }
    else if (DropDownList1.SelectedValue == "kit_ru@hotmail.com")
    {
        Button1.BackColor = System.Drawing.Color.Black;

    }

a drop down list: 下拉列表:

<asp:DropDownList ID="DropDownList1" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" runat="server">
    <asp:ListItem value="blue">amir.tuchner@gmail.com</asp:ListItem>
    <asp:ListItem Value="Green">alon_gk@netvision.net.il</asp:ListItem>
    <asp:ListItem Value="Red">ohad_jl@internet-zahav.co.il</asp:ListItem>
    <asp:ListItem Value="yellow">nirho_fg@walla.com</asp:ListItem>
    <asp:ListItem Value="white">yidhj_ry@yahoo.com</asp:ListItem>
    <asp:ListItem Value="black">kit_ru@hotmail.com</asp:ListItem>
</asp::DropDownList>

And a button: 和一个按钮:

enter code here<asp:Button ID="Button1" runat="server" style="background-color:aqua" Text="Send and Receive" BackColor="#FF3399" />

What can I do to make it work? 我该怎么做才能使其正常工作?

In your DropDownList you have set the AutoPostBack property to true . 在您的DropDownList中 ,将AutoPostBack属性设置为true So every time you change the selection, a postback to the server will be make. 因此,每次更改选择时,都会回发到服务器。

This will force the page to reload. 这将迫使页面重新加载。

But you have defined an inline style for your Button . 但是,您已经为Button定义了嵌入式样式。 Every inline style defined will override the styles you have made by your postback, because the page is rendered and then the styles will be applied. 定义的每个内联样式都将覆盖您的回发样式,因为将呈现页面,然后将应用这些样式。

In your CodeBehind, you check for DropDownList1.SelectedItem.Text and for some conditions you check for DropDownList1.SelectedValue . 在您的CodeBehind中,检查DropDownList1.SelectedItem.Text,并在某些情况下检查DropDownList1.SelectedValue

Just update your code... 只需更新您的代码...

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{        
if (DropDownList1.SelectedItem.Text == "jim.rumdner@gmail.com")
        {
            Button1.BackColor = System.Drawing.Color.Blue;
        }
        else if (DropDownList1.SelectedItem.Text == "alon_gk@netvision.net.il")
        {
            Button1.BackColor = System.Drawing.Color.Green;
        }
        else if (DropDownList1.SelectedItem.Text == "ohad_jl@internet-zahav.co.il")
        {
            Button1.BackColor = System.Drawing.Color.Red;
        }
        else if (DropDownList1.SelectedItem.Text == "nirho_fg@walla.com")
        {
            Button1.BackColor = System.Drawing.Color.Yellow;
        }
        else if (DropDownList1.SelectedItem.Text == "yidhj_ry@yahoo.com")
        {
            Button1.BackColor = System.Drawing.Color.White;
        }
        else if (DropDownList1.SelectedItem.Text == "kit_ru@hotmail.com")
        {
            Button1.BackColor = System.Drawing.Color.Black;

        }
}

Instead of using a lot of if/else, you should use the switch statement adn check for the DropDownList1.SelectedValue ... 而不是使用很多if / else,应该使用switch语句adn check来检查DropDownList1.SelectedValue ...

switch (DropDownList1.SelectedValue)
        {
            case "blue":
                Button1.BackColor = System.Drawing.Color.Blue;
                break;
            case "green":
                Button1.BackColor = System.Drawing.Color.Green;
                break;
            case "red":
                Button1.BackColor = System.Drawing.Color.Red;
                break;
            case "yellow":
                Button1.BackColor = System.Drawing.Color.Yellow;
                break;
            case "white":
                Button1.BackColor = System.Drawing.Color.White;
                break;
            case "black":
                Button1.BackColor = System.Drawing.Color.Black;
                break;
            default:
                Button1.BackColor = System.Drawing.Color.Gray;
                break;
        }

... this will make it easier to read and maintain. ...这将使阅读和维护更加容易。 Finally, remove the inline style of your button. 最后,删除按钮的内联样式。

<asp:Button ID="Button1" runat="server" Text="Send and Receive" BackColor="#FF3399"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据asp.net中的下拉列表选择更新图像 - Updating image based on drop down list selection in asp.net 如何根据另一个下拉列表 ASP.NET 的选择填充下拉列表 - How do I fill a drop down-list based on selection by another drop-down list ASP.NET 如何在折线图中添加下拉列表以在 ASP.NET Web 应用程序中使用 chartJS 进行更改 - How to add a drop down list in a line chart to change using chartJS in ASP.NET web application 如何使用asp.net中的另一个下拉列表更改下拉列表选择的索引和可见性? - How to change drop down list selected index and visibility using another drop down list in asp.net? 我需要选择下拉列表值时,复选框控件应在asp.net中动态更改 - i need that on selection of drop down list value the check box controls should change dynamically in asp.net 如何根据ASP.NET中另一个下拉列表中的用户选择禁用下拉列表项? - How do I disable a dropdownlist item based on a user selection on another drop down in ASP.NET? ASP.NET Web控件下拉列表对象填充 - ASP.NET web control drop down list object population asp.net中下拉列表的选定索引更改事件 - Selected Index change event of drop down list in asp.net 索引更改不起作用 asp.net 中的下拉列表 - On index change not working drop down list in asp.net 如何基于GridView选择更改下拉列表 - how to change drop down list based on gridview selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM