سؤال

I've been trying, unsuccessfully, to create a custom server control that can allow me to take advantage of ViewState and AutoPostBack. What is tricky is that my control is a combination of HTML and JavaScript. Here is a good example of what the rendered output would be (I'm using a JS library as well, hence the abbreviated example):

<input type="text" id="txt" />
<script>
$(function() {
    $("#txt").TurnMeIntoDatePicker({
        value: new Date(1950,1,1)
    });
});
</script>

In this example the "id" of the input and the "value" of the JS function would need to be exposed as parameters (and their values maintained after postbacks via ViewState). Every example I've seen allows you to assign parameters to the input element, but not to some custom string of JS. I would also love to see how you could add an AutoPostBack function, but would just be gravy!

هل كانت مفيدة؟

المحلول

Are you implementing IPostBackDataHandler? You can use it to retrieve the value from the input field on postback. Also, you're sample doesn't show a name in the input field... you'll need to output a name in order for the value to be posted back correctly. Check out the sample here...

http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackdatahandler.aspx

You can use Page.ClientScript.GetPostBackEventReference(...) to perform an AutoPostBack. For example, if you wanted to postback when the value was changed, you could render something like this:

protected override void Render(HtmlTextWriter writer)
{
    string content = string.Format("<input type=\"text\" id=\"txt\" onchange=\"{0}\" />", Page.ClientScript.GetPostBackEventReference(this, null));
    writer.Write(content);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top