Accessing value of a hidden field on Masterpage from codebehind

后端 未结 4 557
失恋的感觉
失恋的感觉 2021-01-06 16:40

In a follow up to my previous question, I want to get the value of the hidden input field from the child page codebehind.

I tried HtmlInputHidden hdnID = (H

相关标签:
4条回答
  • 2021-01-06 17:14

    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.

    0 讨论(0)
  • 2021-01-06 17:16

    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.

    0 讨论(0)
  • 2021-01-06 17:17

    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");
    
    0 讨论(0)
  • 2021-01-06 17:25

    Use something like:

    if (Page.Master.FindControl("hdnField") != null)
    {
        String myValue = (HtmlInputHidden)Page.Master.FindControl("hdnField").value;
    }
    
    0 讨论(0)
提交回复
热议问题