开发者

How to add hidden field in form element in masterpage

开发者 https://www.devze.com 2023-02-06 00:17 出处:网络
I want to add hidden field to every view i have through jquery or javascript. But i want that code to be in MasterPage.Master so i write code at one place and it adds on every view i have. Can i do th

I want to add hidden field to every view i have through jquery or javascript. But i want that code to be in MasterPage.Master so i write code at one place and it adds on every view i have. Can i do this if yes then how?开发者_运维问答 Im using asp.net mvc 2


In your master:

$(function() {
    $('body').append(
        $('<input/>')
            .attr('type', 'hidden')
            .attr('name', 'foo')
            .val('some value')
    );
});

or replace $('body') with some other selector to a placeholder you've put somewhere in your master page if you want this hidden field to be inserted in some specific position. You could also insert it into existing <form> by giving them some id or class (if you have more of them on the same page and you wanted to insert this hidden field in each form).


Not sure about MVC but in the master page Page_PreRender method you can have such code:

HiddenField field = new HiddenField();
field.ID = "HiddenField1";
field.Value = "SomeValue";
(this.Page.FindControl("form1") as HtmlForm).Controls.Add(field);

This will push the hidden field to the form regardless of what page is using the master page.

0

精彩评论

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

关注公众号