如何单击菜单按钮并更改颜色 [英] How Do I Click The Menu Button And Change Color

查看:90
本文介绍了如何单击菜单按钮并更改颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button1_Click(object sender, EventArgs e)
{
    Button1.Attributes.Add("style", "background-color: #0066FF");
}



我点击按钮但不改变颜色,但我点击两次更改颜色。但我想要更改颜色首先单击


i click button but dont change color but i click twice change color. But i want change color first click

推荐答案

事件实际发生在控件的渲染之后。此更改仅在第二页重新加载时被选中,因此渲染器知道添加此属性。



而是使用javascript在客户端进行更改或尝试拦截页面加载或init上的事件。



除非你正在执行部分回发,否则在页面重新加载后在客户端进行更改将不会保留:如何使用AJAX UpdatePanel启用部分渲染 [< a href =http://www.codeproject.com/Tips/656031/How-to-enable-partial-rendering-with-the-AJAX-Updatarget =_ blanktitle =New Window> ^ ]。该按钮必须是一个外部触发器,否则它将被重新渲染。



拦截按钮单击页面加载或初始化并不是太棘手。您需要获取 Request.Form [__ EVENTTARGET]; 这是触发回发的控件的唯一ID。我在迭代中继器中使用它,因为我更喜欢在c#中使用我的所有后端代码。我几乎没有在标记中使用asp.net。



您的代码可能如下所示:

The events actually take place after the render of the control. This change would only be picked up on the second page reload so the render knows to add this attribute.

Instead make the change at the client side using javascript or try to intercept the event on page load or init.

Making the change on the client side would not persist after a page reload unless you are performing a partial postback: How to enable partial rendering with the AJAX UpdatePanel[^]. The button would have to be an external trigger otherwise it would get re-rendered.

Intercepting the button click on page load or init isn't too tricky. You need to get the Request.Form["__EVENTTARGET"]; which is the unique id of the control that triggered the postback. I use this in itembound repeaters because I prefer to have all of my backend code in c#. I hardly ever use asp.net in the markup.

Your code might look like this:
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    var requestTarget = Request.Form["__EVENTTARGET"];

    var btnUniqueId = Button1.UniqueID;

    if(requestTarget == btnUniqueId)
         Button1.Attributes.Add("style", "background-color: #0066FF");
}





这将设置按钮颜色一次。这个例子没有办法重置它,但它可以做得很简单。





希望有所帮助^ _ ^

Andy



This will set the button colour once. There is no way included in this example to reset it, but it can be done simply enough.


Hope that helps ^_^
Andy


这篇关于如何单击菜单按钮并更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆