开发者

Pass parameter to databind from web forms

So I have a Web Forms asp control:

<asp:DropDownList runat="server" ID="ExistingTemplate" ClientIDMode="Static" />

On the backend code, it gets populated through this:

public override void DataBind()
    {
        base.DataBind();

        var selectList = Chatham.Web.Models.Indications.DropDownData.AllEditableTemplates();
        ExistingTemplate.DataSource = selectList.Items;
        ExistingTemplate.DataTextField = selectList.DataTextField;
        ExistingTemplate.DataValueField = selectList.DataValueField;
        ExistingTemplate.DataBind();


        SetTabVisibility();
    }
开发者_如何学编程

Now, I want to refactor the AllEditableTemplates method to take a parameter. This parameter is only accessible through the client side Javascript code, on the master page front end.

How can I pass a parameter to this method that I get from Javascript on the page?


You could use JavaScript to store it in a hidden form field. Then it would be easy to retrieve it from the server-side using the HttpRequest.Form property.


Expose it as a web method and have javascript call it, instead of it trying to get the value from javascript. http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx

Using a hidden field has a problem - normally, data bind is called right away somewhere, which means the initial request to the web server didn't have the form field set yet and it will be empty. You would need a second request in order for it to populate so that your form request had it. Calling the web method from the client instead solves this problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜