How to set value in master page and access that value

Posted by Klbaiju under ASP.NET on 3/31/2015 | Points: 10 | Views : 437 | Status : [Member] | Replies : 1
Hi,

in my applicattion ,iam using master page.

i have added a hiddenfield in master page . its id is Huname.

my startup page is Login.aspx.

after checking username and password in Login.aspx, i have set the value for hiddenfield in master page as

string uname = txtuname.Text;
string password = txtpassword.Text;

HiddenField Hname = (HiddenField)Page.Master.FindControl("Huname");
Hname.Value = uname;


when i tried to access the hiddenfield value in masterpage it is showing null.

how to solve this

Regards

Baiju




Responses

Posted by: Rajnilari2015 on: 4/1/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Try this

if (Page.Master.FindControl("Huname") != null)

{
HiddenField Hname = (HiddenField)Page.Master.FindControl("Huname");

Hname.Value = uname;
}


Additionally you can expose a public property in your master page and can set the value as below

Let us say your master page is call MyMaster. And say the hidden field id is "myHiddenField"

Expose a public property in MyMaster as

public String MyHiddenFieldValue { 

get{return myHiddenField.Value;}
set{myHiddenField.Value = value;}
}

And then you can do

MyMaster master = (MyMaster)Page.Master;

string uname = txtuname.Text;
string password = txtpassword.Text;
master.MyHiddenFieldValue = uname


--
Thanks & Regards,
RNA Team

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response