Accessing value of a hidden field on Masterpage from codebehind

早过忘川 提交于 2019-11-30 23:17:55

So change it FROM

HtmlInputHidden hdnID = (HtmlInputHidden)Page.Master.FindControl("ctl00_hdnField");

TO

HiddenField hdnID = (HiddenField)Page.Master.FindControl("hdnField");

It's just a casting thing - notice HtmlInputHidden changed to HiddenField. You also don't need the ct100_ part - this is just so the HTML rendered element has a unique ID.

The control on your page is an asp.net control, not a generic HTML control.

You would use HtmlInputHidden if you put a generic <input type="hidden" /> in your HTML.

You should create a property in Masterpage which wrap the HiddenField.

public String HdnFieldValue
{
get
{
    return hidField.Value;
}
set
{
    hidField.Value = value;
}
}

And in page code behind you can access it like this:

((YourCustomMaster)Page.Master).HdnFieldValue

If something is not clear please ask me.

I don't think you need to prefix the hidden field's ID with ctl00_, just use the normal ID:

(HtmlInputHidden)Page.Master.FindControl("hdnField");

Use something like:

if (Page.Master.FindControl("hdnField") != null)
{
    String myValue = (HtmlInputHidden)Page.Master.FindControl("hdnField").value;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!