开发者

Dynamic javascript from web resource

I have written a custom web control that validates some input to a textbox client-side. The associated JavaScript is an embedded resource. As part of the validation 开发者_如何学运维process the JavaScript displays a confirm alert box.

I want to be able to set the message of the alert from a property of web control, in a similar way to the standard asp.net validation controls have the ErrorMessage property.

Can anyone advise me on the best way to splice this property into my embedded JavaScript?

Embedded js function

function checkDeduction(sender, eventArgs) {
    var dNewVal = eventArgs.get_newValue();
    if (dNewVal > 0) {
        var bRetVal = confirm("custom msg here");
        if (!bRetVal) {
           dNewVal = dNewVal * -1;
           sender.set_value(dNewVal);
        }
    }
}


To achieve this I added a custom attribute to my custom control on the PreRender event:

protected override void OnPreRender(EventArgs e)
{
        this.Attributes.Add("ErrorText", "custom error text from public prop");
        ...
        base.OnPreRender(e);
}

I was then able to hold of the error message client-side like so:

confirm(sender.getAttribute('ErrorText'));

Job done!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜