Monday, March 26, 2012

Passing Variable To Controls

If we have a web page in which any simple control is present for example a TextBox

<asp:TextBoxID="TextBox1"runat="server"Text=""></asp:TextBox>

Now in code file a public variable is defined like

publicstring txtValue ="Khan";

Now I want to assign thetxtValue inText filed of TextBox Just like

<asp:TextBoxID="TextBox1"runat="server"Text="<%=txtValue%>"></asp:TextBox>

But this approach is not working.

Can anyone tell it?

Modify your markup to:

<asp:TextBox ID="TextBox1" runat="server" Text='<%# GetValue %>' />
 Then you should make an at least protected function in your code behind to return the string:
 
protected string GetValue(){return txtValue;}

I have tried it but it is getting error.

Please check it on you own side.


This syntax should work. I've used it a dozen times in my apps.

What is the error you're getting?


Default.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclass_Default : System.Web.UI.Page

{

privatestring txtValue ="Zeeshan";protectedstring GetValue()

{

return txtValue;

}

}

Default.aspx

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"Debug="true" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue %>"></asp:TextBox>

</div>

</form>

</body>

</html>

And the error which I am getting is

Error 1 The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments D:\New Project\dawateislamix.net\Default.aspx 12

Error 2 Argument '1': cannot convert from 'method group' to 'object' D:\New Project\dawateislamix.net\Default.aspx 12


Can you try with<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue() %>"></asp:TextBox>


Modify your html as follows:

<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue() %>"></asp:TextBox>


There is not any error now.

But still the value is not passed to the control.


Pls try to set the value in

Page_Load() event.

txtValue ="Zeeshan" ;


Default2.aspx

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:textboxID="Textbox1"runat="server"Text="<%# GetValue() %>"></asp:textbox>

</div>

</form>

</body>

</html>

Default2.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclassDefault2 : System.Web.UI.Page

{

privatestring txtValue;protectedstring GetValue()

{

return txtValue;

}

protectedvoid Page_Load(object sender,EventArgs e)

{

txtValue ="Zeeshan";

}

}

Please check it.

It still does not show the text.


sorry guys its as simple as this

in your code behind you have this

publicpartialclasscodedisplay : System.Web.UI.Page

{

privatestring txtValue ="Zeeshan";protectedvoid Page_Load(object sender,EventArgs e)

{

TextBox1.Text = txtValue;

}

}

and in your aspx you have this

<asp:TextBoxID="TextBox1"runat="server"/>

with all form and html elements.

Good luck


You can do it in your code behind instead of in your markup:

protectedvoid Page_Load(object sender,EventArgs e)

{

if (!IsPostBack)

TextBox1.Text = GetValueStr();

}

privatestring txtValue ="test";protectedstring GetValueStr()

{

return txtValue;

}


But If I want to do it on Mark up then.


Well if you want to do it on mark up then, put it straight in

text="Zeeshan"

text is a property and hence

Don't use code blocks in ASPNET. Avoid if you can avoid it because they are written for backward compatability; instead write an event handler or startup routine in the codebehind which populates the property on the control as necessary.

For example, you could do it in Page_Load - but be sure to surround it with if (! PostBack) so that the value doesn't get overwritten when posting back.

In late binding controls i.e. say a textbox in a gridview etc, you can databind but that again uses reflection etc and hence is slow.


Hi,

If you have to do it in the markup use single quotes instead Text='<%=MyPublicVariable %>'

HTH

0 comments:

Post a Comment