1

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.

2
  • So to clarify, you run SQL and get a list of string (or int, or whatever) values corresponding to your list of buttons. Then, for each value in the list, you use some kind of logic to determine how that value should be displayed (for example, it could be formatted as a date, a count of upcoming events, plain text, or whatever). Is that right? Or are you trying to get the text of these buttons to change in real-time as the user watches? Just trying to understand what you mean by "dynamic". Commented Jan 11, 2011 at 19:19
  • Another possibility: Are you talking about completely arbitrary code that could be completely different from one button to the next? Like a custom function to go with every button? Commented Jan 11, 2011 at 19:44

4 Answers 4

2
+50

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

Sign up to request clarification or add additional context in comments.

Comments

0

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())

5 Comments

well that is not exactly the issue. The issue is that given a dynamic button, I need to look at it and determine if there is dynamic text, such as needing a count of birthdays, and then get that information.
@Wade73: then you should show us more code, so that we can see how you are creating these buttons and where you have problems with.
I am trying to decide how to architect the solution, so there is really nothing to show yet.
@Wade73: do you want to build a dynamic jQuery Menu? How many items/buttons do you need to create? If the number is not too high i would preload all dynamic texts the same time you load the menus from database with the same query if possible.
I am not sure what to build yet, just looking for people who have had experience trying to deal with this type of issue.
0

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.

Comments

0

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.

2 Comments

Unfortunately, as I understood, he doesn't only need to set a custom text but to run custom code when the button is clicked!! :( :(
In that case, the button should be passing parameters to pre-existing code, not carrying its own payload of code around with it. You're going to have to write the code at some point anyway (unless you want to give users the ability to run their own code, which is terrifying), so you may as well put it in the .aspx and .cs files with everything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.