I have a function which sets my linkbutton as the default button for a panel.
protected void Page_PreRender(object sender, EventArgs e)
{
string addClickFunctionScript = @"function addClickFunction(id) {
var b = document.getElementById(id);
if (b && typeof(b.click) == 'undefined')
b.click = function() {
var result = true;
if (b.onclick) result = b.onclick();
if (typeof(result) == 'undefined' || result)
eval(b.getAttribute('href'));
}
};";
string clickScript = String.Format("addClickFunction('{0}');", lbHello.ClientID);
Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + lbHello.ClientID, clickScript, true);
}
This works fine. How to make this reusable to all my pages of my application. One page can have multiple linkbuttons and multiple panels…. Any suggestion…
The cleanest way would be to use a custom server control that inherits from
LinkButton. In fact this seems to be in line with the blog post from your earlier question. All you need to do is override theOnPreRenderevent and paste the code you have while changinglbHello.ClientIDtothis.ClientIDto refer to the specific instance of that control. It should not take more than 10 minutes to set this up. Once this is done, you can use as many of the controls as you want on one page and easily support it throughout your application’s various pages.You might find this MSDN article helpful when following my instructions below, specifically the “Creating the Server Control” section: Walkthrough: Developing and Using a Custom Web Server Control. Here’s a step by step guide to accomplishing this:
LinkButtonDefault(you’re free to change the name, of course).ServerControl1.cstoLinkButtonDefault.csCustomControlsAssemblyInfo.csfile (contained in thePropertiesfolder of the project). Add this line at the bottom of the file:[assembly: TagPrefix("CustomControls", "CC")]LinkButtonDefault.csadd this code to override theOnPreRenderevent:Code (notice the use of
this.ClientID):You may also want to update the generated attribute code above the class declaration that starts with
[ToolboxData("<{0}:to useLinkButtonDefaultinstead ofServerControl1. That’s it for the new Server Control project. I highly recommend reading the aforementioned MSDN article to take advantage of other capabilities, such as adding controls to the toolbox if you have a need to do so.After completing these steps you should have a
LinkButtonDefault.csfile that resembles this:Now return to your web application and add a reference to the
CustomControlsproject. You should be able to do this from the Add Reference’sProjecttab since I suggested adding the above project to your existing solution. If you want you could’ve built the above project in its own solution then you would add a reference to it’s.dllfile by using theBrowsetab. Once a reference has been added you are ready to use the newLinkButtonDefaultcontrol.To use the controls you can use the @ Register directive on each page the control will be used, or you can add it to the Web.config and gain easy reference to it throughout your application. I will show you both methods below. Based on your question I think you’ll want to add it to the Web.config. Refer to the MSDN article and you will find this information half way down the page under “The Tag Prefix” section.
Using @ Register directive:
Go to your desired
.aspxpage and add theRegisterdirective to the top of each page you want to use the control in:On the same page, you may now use multiple instances of the control. Here’s an example:
In the code behind (
.aspx.cs) you would need to add:Using Web.config:
To use the Web.config keep the exact same markup and code used in the above example. Follow these steps:
@ Registerdirective used on the .aspx markup.Web.configfile for your web application.<system.web>...</system.web>section.Mapping:
Recompile and everything should build successfully. With this in place you no longer need to specify the
@ Registerdirective on each individual page.If you get stuck and have any questions let me know. Just read over everything above carefully since it’s a long post with lots of code.