adding variables to Server tag [duplicate] - html

This works:
<span value="<%= this.Text %>" />
This doesn't work:
<asp:Label Text="<%= this.Text %>" runat="server" />
Why is that?
How can I make the second case work properly, i.e., set the label's text to the value of the "Text" variable?

Use Data binding expressions
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" ></asp:Label>
Code behind,
protected void Page_Load(object sender, EventArgs e){
DataBind();
}

you can do this
<asp:Label ID="Label1" runat="server" ><%= variable%></asp:Label>

You will need to set the value of the server control in code
First of all, assign an ID to the label control so you can access the control
<asp:Label ID="myLabel" runat="server" />
Then, in your Page_Load function, set the value of your labels 'Text' field
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = 'Whatever you want the label to display';
}
This function will be in your code behind file, or, if you are not using the code behind model, inside your aspx page you will need
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = 'Whatever you want the label to display';
}
</script>
Good luck.

In my code i am using something like this easily but in the databound control like ListView Item template
<asp:HyperLink ID="EditAction" class="actionLinks" Visible='<%#Eval("IsTrue").ToString() != "True"%>' runat="server" NavigateUrl='<%# Eval("ContentId","/articles/edit.aspx?articleid={0}")%>' />
But when i tried to use outside the databound control using <%# .. %>, it simply doesn't work.
You can easily do with
My href
But for server controls, and outside of databound control. We need to call DataBind() in pageload event explicitly
<asp:Hyperlink ID="aa" NavigateUrl='<%#myHref%>' >

Not sure how to mark this as such, but this is a bit of a duplicate. See this thread.
I don't think embedding code in to your markup will really make your markup any clearer or more elegant.

<asp:Label> is compiling at runtime and converting to html tags. You can set text with codebehind or like this:
<asp:Label id="Text1" runat="server" />
<% Text1.Text = this.Text;%>
UPD: Seems like my variant doesnt work, this is better:
protected void Page_Load(object sender,EventArgs e)
{
Text1.Text = this.Text;
}

Just pitching this little nugget in for those who want a good technical breakdown of the issue -- https://blogs.msdn.microsoft.com/dancre/2007/02/13/the-difference-between-and-in-asp-net/
I think the crux is in pretty decent agreement with the other answers:
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
<%# expressions can be used as properties in server-side controls. <%= expressions cannot.

Related

How to Pass Text Box Value to Query String Using HyperLink Field In Client Side Using Asp.Net

Iam developing application in asp.net c#, in that i have a requirement to pass text box value through query string without using server side code,
in that iam trying some code below, but its not working for me.
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=<%#Eval('txtSerialNo.ClientID')%>" ToolTip="View History"></asp:HyperLink>
When iam assign value directly like the below code its working fine.
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=1" ToolTip="View History"></asp:HyperLink>
Note: i need not server side code to pass value
like
protected void lnkNavigate_Click(object sender, EventsArgs e)
{
Response.Redirect("MyLocation.aspx?value=" + myTextBox.Text, false);
}
if it is not possible means please tell me is there any another option to pass text box value to query string like a href & link button
any help would be appreciated.. thanks in advance..
If you want to still use the asp:HyperLink then you can use this snippet.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=" ToolTip="View History" onclick="setHyperlink()"></asp:HyperLink>
<script type="text/javascript">
function setHyperlink() {
var value_hyperlink = $("#<%=hlnkHistory.ClientID %>").attr("href");
var value_textbox = $("#<%=TextBox1.ClientID %>").val();
$("#<%=hlnkHistory.ClientID %>").attr("href", value_hyperlink + value_textbox);
}
</script>
If you don't need that hyperlink, you can use a somewhat simpler javascript function.
location.href = "~/WebFormReports/History.aspx?SerialNo=" + $("#<%=TextBox1.ClientID %>").val();

How to access Input type="Checkbox" control inside <dx:ASPxGridView/>(Dev Express) at server side(c#)

I have a dx:ASPxGridView (Dev express gridview) which contains HTML checkboxes - input type="checkbox" control.
Problem is how can i access these HTML checkboxes at server side, so that i can check those checkboxes according to condition.
Please suggest me how can i access these HTML checkboxes on server side.
Code image can be found here
Thanks
Girish Rawat
First, to access a HTML element on a server, you need to mark it with the runat="server" attribute.
To access your checkbox, handle the ASPxGridView.HtmlDataCellPrepared event. In the event handler, you can get your input by using the ASPxGridView.FindRowCellTemplateControl method. You will also need to set ID for the input.
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" OnDataBinding="ASPxGridView1_DataBinding" OnHtmlDataCellPrepared="ASPxGridView1_HtmlDataCellPrepared" KeyFieldName="ID">
<Columns>
<dx:GridViewDataCheckColumn VisibleIndex="0">
<DataItemTemplate>
<input id="check" type="checkbox" runat="server" />
</DataItemTemplate>
</dx:GridViewDataCheckColumn>
</Columns>
<Styles AdaptiveDetailButtonWidth="22"></Styles>
</dx:ASPxGridView>
protected void ASPxGridView1_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) {
HtmlInputCheckBox check = (HtmlInputCheckBox)ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "check");
check.Checked = GetValue((int)e.KeyValue);
}
public bool GetValue(int id) {
// your logic
return id % 2 == 0;
}

unable to get checked property of htmlinoutcheckbox on server side(code behind)

I have created a html input check-boxes dynamically from code behind and after rendering the check-boxes on to aspx page I'm unable to get the checked property of those check-boxes on button click event. week is the enum with alldays of a week. Here the sample code.
HtmlInputCheckBox chkbx = new HtmlInputCheckBox();
chkbx.Attributes.Add("id", ((week)i).ToString());
chkbx.Attributes.Add("runat", "server");
chkbx.Attributes.Add("name", ((week)i).ToString());
chkbx.Attributes.Add("value", "checked");
HtmlGenericControl label = new HtmlGenericControl("label");
label.Attributes.Add("for", ((week)i).ToString());
if (i == 1 || i == 7)
{
label.Attributes.Add("class", "dow disabled");
label.Attributes.Add("disabled", "true");
}
else
{
label.Attributes.Add("class", "dow");
chkbx.Checked = true;
}
label.InnerText = ((week)i).ToString().Substring(0,2);
_dowcontrol.Controls.Add(chkbx);
_dowcontrol.Controls.Add(label);
ASPX page
<body>
<form id="form1" runat="server" method = "post">
<t2:mycontrol ID="SampleControl" runat="server" >
</t2:mycontrol>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>
</body>
ASPX.CS page
What should be inside button click?
Tried
Request.Form["***"], FindControl("***")
In order for your dynamically created controls to interact with viewstate, events and other stages in the page life-cycle, they need to be created in the Init event. Creating these controls later in the life-cycle will exclude them from taking part in post value binding, viewstate binding, etc. Also note that you must recreate the controls on EVERY postback.
protected void Page_Init(object sender, EventArgs e)
{
// Do any dynamic control re/creation here.
}

How to access the text property in masterpage so that navigation to other pages in website is possible?

I want to redirect to another page in my website. I have a textbox and a search button in my masterpage.The txt1.text should be the redirection url to be used to navigate to other pages. But i am unable to access the value in txt1.txt in my masterpage.
Please find the code below:-
<asp:TextBox runat="server" ID="txt1" Height="22px" Width="139px" class="searchtextbox"></asp:TextBox>
</div>
<div class="searchicon">
<asp:Button ID="Button5" runat="server" Text="Search" class="searchicon" onclick="Button5_Click"/>
</div>
In my masterpage, i have the code as follows:
protected void Button5_Click(object sender, EventArgs e)
{
{
Response.Redirect("txt1.Text"+".aspx");
}
}
but the value in txt1 is null..
Please help me.

updatepanel __dopostback hits .update() but does not refresh

I have a page with two update panels each of which has a gridview generated dynamically. Updatepanel1 refreshes every ten seconds on a timer. The second update/grid refreshes when an item is selected in the first grid.
I'm trying to accomplish this feat using __doPostBack. This method does reach the server and run my .update on updatepanel2. I see that updatepanel2 gets data, but the form never actually updates updatepanel2.
I can get updatepanel2 to display data only when updatepanel1 timer ticks and I set updatepanel2 mode to "Always".
anyone have any suggestions?
Thanks
Well I fixed this problem. I modified to using the following method for the doPostBack call.
http://encosia.com/2007/07/13/easily-refresh-an-updatepanel-using-javascript/
Hope this helps.
I see you answered your own question, but for the benefit of others, instead of doPostBack(), why not set both on a Timer to refresh at a specified interval, or as a Tick event method with an "UpdatePanel1.Update()" at the end of the method? For this way, you'd need to set the interval in the Default.aspx page code itself; I picked 10ms so it could show the progress for a very fast operation:
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="360000" />
<asp:Button ID="btnDoSomething" runat="server" Text="Do Something" OnClick="btnDoSomething_Click" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load" UpdateMode="conditional">
<ContentTemplate>
<span id="spnLabel" runat="server">
<asp:Timer ID="Timer1" runat="server" Interval="10" OnTick="Timer1_Tick"></asp:Timer>
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
Then have a Timer1_Tick method in the code-behind that you call when you have an update - in this example, something to add to spnLabel.InnerHtml from a btnDoSomething_Click() method:
protected void btnDoSomething_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
Timer1.Interval = 10;
Timer1_Tick(sender, e);
Timer1.Enabled = false;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
spnLabel.InnerHtml = "hi";
UpdatePanel1.Update();
}
Remember that the refresh is controlled by the Timer interval, not by when you call Timer1_Tick(sender,e), even if you have an UpdatePanel1.Update() at the end - example, if you set the interval to 10000, it would update after 10 seconds, even if your operation had used the Timer1_Tick() method several times before then. You would still need an UpdatePanel1.Update() at the end, regardless, though.
-Tom