开发者

ASP.Net: Programatic runat="server" and onclick

开发者 https://www.devze.com 2022-12-14 23:44 出处:网络
I am trying to edit开发者_运维百科 a control which doesn\'t have an .ascx page, it was done completely in code. I am trying to add an optional delegate to be called from an ImageButton which was previ

I am trying to edit开发者_运维百科 a control which doesn't have an .ascx page, it was done completely in code. I am trying to add an optional delegate to be called from an ImageButton which was previously completely handled at the client. So I would like to set runat="server". However, I don't see this property exposed programatically on the object!


If you're creating it in code, it is implicitly runat="server", no need to specify.


Server controls automatically run on the server, so I'm assuming you'd like help tying this all together.

If you are able to modify the control you can add an event that behaves like a normal event but calls a delegate. Sorry, I'm not familiar with the exact C# syntax so I will show VB. In the control:

Public ParentDelegateEvent As System.Delegate

Public Sub btnDelegateClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelegate.Click

    Dim args(1) As Object
    args(0) = sender
    args(1) = e
    ParentDelegateEvent.DynamicInvoke(args)

End Sub

Then in the page that has this control on it, setup the delegate in the PageLoad event:

Dim delMyEvent As New DelegatePostBack(AddressOf MyPageEventHandler)
MyControl.ParentDelegateEvent = delMyEvent

MyPageEventHandler is defined on the page like anything else:

Private Sub MyPageEventHandler(ByVal sender As Object, ByVal e As EventArgs)

End Sub


Maybe this will be helpful. I have a custom control (all code, as yours is) that I do something like this to add the click:

myLinkCtrl = new myLinkControlClass();
myLinkCtrl.Click += myEventName;
myLink.PostBackUrl = myTargetURL;
myLink.Attributes.Add([add some attributes here like target, etc.]);
etc..

myLinkControlClass is a custom class that inherits LinkButton and implements some custom interfaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消