简体   繁体   English

如何设置用户控制按钮的背景色? 以及如何将参数传递到其事件处理程序?

[英]How to set the background color for a user control button? and how to pass in parameters to its event handler?

I created a user control for a button because I will be needing to use it multiple times in the application. 我为按钮创建了一个用户控件,因为我将需要在应用程序中多次使用它。

<td class = "center" colspan = "4"><uc:Instructions ID="Instructions" runat="server" />

I have this in the ascx file of the button 我在按钮的ascx文件中有这个

<asp:Button ID ="btnInstrs" Text ="Instructions" runat ="server" CssClass ="buttonGreen" OnClick ="btnInstrs_onClick"/>

This is the code behind for the user-control 这是用户控件后面的代码

    protected void btnInstrs_onClick(object sender, EventArgs e)
{

}

public void setColorGreen()
{
    btnInstrs.BackColor = System.Drawing.Color.Green;
}

I have this as the CSS 我有这个作为CSS

.buttonGreen
{
background-color: green;
}

So far I have this in the code behind in one of the pages I will be needing this button 到目前为止,我在其中一页的背后代码中都拥有此功能,我将需要此按钮

 Instructions.setColorGreen();

The setColorGreen method is changing the background color to green when I call it. setColorGreen方法在我调用它时将背景色更改为绿色。 Any idea why using just the CSS isn't doing the job? 知道为什么只使用CSS不能完成这项工作吗?

Now to my main question. 现在是我的主要问题。

I want to return some instructions as a popup and some text on the page when the button is clicked. 单击按钮后,我想返回一些说明作为弹出窗口,并在页面上返回一些文本。 to get those instructions I will need an itemID to look in the DB and get the instructions So if I was doing everything in the source page. 要获得这些说明,我将需要一个itemID在数据库中查找并获得说明。因此,如果我在源代码页面中进行了所有操作。 In the event handler I would make a DB call for the item ID and find the instructions and display but because I want to do it using the user control I don't know how to pass in the itemID or anything else as the parameter into the user control event handler. 在事件处理程序中,我将对项目ID进行数据库调用,并找到说明和显示内容,但是由于我想使用用户控件进行操作,因此我不知道如何将itemID或其他任何参数作为参数传递给用户控件事件处理程序。

Any advise as to how I could code the event handler so I can re-use it in different pages to get the instructions(for an itemID) would be greatly appreciated. 任何有关如何编码事件处理程序的建议都将不胜感激,以便我可以在不同页面中重复使用它以获取指令(针对itemID)。

Calling a method in parent page from user control 从用户控件中调用父页面中的方法

This answers my second question. 这回答了我的第二个问题。 Create an event for the user control when clicked. 单击时为用户控件创建一个事件。 Handle the event in the source page when exposed. 暴露时在源页面中处理事件。

I can use my itemID in the event handler to make necessary database calls and retrieve information and publish the instructions. 我可以在事件处理程序中使用我的itemID进行必要的数据库调用并检索信息并发布说明。

I do the same thing for any other pages required. 对于需要的其他任何页面,我也会做同样的事情。

Still did not understand why CSS isn't setting the background color of the button to Green. 仍然不明白为什么CSS不能将按钮的背景色设置为绿色。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM