Create dynamic button with dynamic text
I have a project where I need to create menu buttons from a list in SQL Server. The problem I am having is that I need to add code to the text of those buttons. So there would be a birthday button and it should display the number of birthdays within the next two weeks or a button with the number of upcoming events. Any thoughts on how best to do this? Thanks.
Wade
Clarification:
There is no code yet, just some requirements. What I am doing is querying a table to get 开发者_开发知识库the list of buttons to display. Now each of these buttons may have dynamic text, for things like count of birthdays, events,etc... I am trying to see what the best way would be to handle this. Should I embed a snippet of code to go along with the menu item to execute when I iterate over the menu items? Maybe I should build a javascript file to go along with the code, which I add code to query a service for certain menu items? Thanks for any help.
what you are asking is close to impossible. Your requirement would be of dynamic coding, ie. the page knows nothing about event handlers before its compilation.
While you can add buttons dynamically, adding code dynamically (under the form of methods) is extremely difficult and represents a potential security hole!!
I said it's close to impossible, but not impossible yet, if you have lots of time to spend.
First: generating those buttons dynamically. Have you ever heard of Repeater
control? I hope so, it allows you to add a button for each element returned by your SQL query. Obviously, you can grab text from the database (do you want to make it localizable? Additional effort is required for that)
Now what? You have to assign a custom behaviour to each of the buttons, and here comes the almost-impossible part.
Idea 1: use reflection and plugin components
"Simply" store in the database the strong name of a class that implements a known method with Action
delegate, or Action<object>
if you need a parameter, and have ASP.NET call that method when the button is pressed by analysing CommandName
and CommandArgument
of Button
's Command
event. Plugins can be stored (that's why you need strong name) inside separate DLLs in the Bin directory, with the possibility to hot-deploy new DLLs
Idea 2: scripting
Use a scripting language like Lua, and store a column about the script to run inside the database. When the button is pressed, run that script through Lua interpreter for C# (if you need the link to it and you are too lazy to do, I can Google around for you). This costs you lots of cross-integration between Lua and .NET, if it's worth the effort
No other ideas yet
If i understand you correctly, you don't have problems with creating the buttons but with their dynamic Text? You should do that in SQL. Are you using SQL-Server?
Something like this:
SELECT COUNT(*) AS Next2WeeksBirthDays FROM TblUser WHERE BirthDay < DATEADD(week, 2, GETDATE())
or
SELECT COUNT(*) AS Next2WeeksEvents FROM TblEvents WHERE TimeStamp < DATEADD(week, 2, GETDATE())
How about using a generic handler, that returns an image, that you manually create there? Use some of the System.Drawing classes to create your image (e.g. load small icons and write text) and simply write its byte[] to the OutputStream.
I'm trying to understand what you're asking here, so let me know if this doesn't answer your question. If you just want to run some logic against the items in your data source before you show them on the page, then the code is fairly simple:
ASPX page:
<asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" />
</ItemTemplate>
</asp:Repeater>
C# Codebehind:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Button buttonCtrl = (Button)e.Item.FindControl("Button1");
string buttonText = GetButtonText(e.Item.DataItem);
buttonCtrl.Text = buttonText;
}
...where GetButtonText is a custom method that decides what the text of Button1 should be, based on the data row or list item being passed in.
If what you need is more complicated than that, respond below and I'll offer a different solution.
EDIT: What about just a library of Web User Controls that can be dynamically added to the page? There'd be no need to store any code in the database that way, just an indicator of which control to use and any parameters to pass in. Many CMS's operate this way.
精彩评论