How to set CSS class to Button control using Session value in design page - asp.net

I want to set CSS class to asp:Button control using Session value. I have tried this, but it does not works.
<asp:Button ID="btnSave" runat="server" Text="<%$ Resources:Application,Save %>" CssClass="<%# Common.SessionInfo.Button %>" ValidationGroup="save"
OnClick="btnSave_Click" />
It works fine, when I set it from code behind
btnSave.CssClass = Common.SessionInfo.Button;
Please help...

You need to call DataBind method from code behind as in code below. When you use expression like <%# %> then you need to call databind on that server control in your code-behind.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.btnSave.DataBind();
}
}

You may create method in your code behind:
public string GetButtonClass(){
return Session["your_key"].ToString();
}
and call this method to your button:
<asp:Button ID="btnSave" runat="server" Text="<%$ Resources:Application,Save %>" CssClass="<%=GetButtonClass()%>" ValidationGroup="save"
OnClick="btnSave_Click" />

Related

Asp.net C# - checkbox control won't stay true after code-behind runs

Front HTML
<asp:CheckBox ID="chkSend" runat="server" Text="Send?" />
The code that triggers the code behind
<asp:DropDownList ID="ddlStatus" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="true"
runat="server" DataValueField="Id" DataTextField="Status" />
The code that runs in the code behind
protected void ddlStatus_SelectedIndexChanged(object s, EventArgs e)
{
this.chkSend.Checked = True;
}
When get back to the front end , the checkbox isn't checked. Any Ideas?
If it helps, this particular view is using Multi-views to which I'm new to.

can't access a property using <%# %>

i was trying to hide and show a button using following code
where AllowUpdate is a property of page.
now problem is This statement never get Executed.
I have used similar code on other pages but it is unreliable many times it just fails and hides buttons even if they must not be
<asp:Button runat="server" ValidationGroup="param" Text='<%$ Resources:Resources, Save%>' ID="btnsave" CssClass="btn btn-primary btn_round" OnClick="btnsave_Click" Visible="<%# AllowUpdate %>" />
If you're going to use the <%# %> syntax, you must call data bind.
<asp:Panel runat="server" ID="Panel1">
<%# SomeProperty %>
</asp:Panel>
Code behind:
Panel1.DataBind();
Alternatively, use the <%= %> syntax.
<%= SomeProperty %>
or as Cal279 points out in the comments, you can set it in your code behind on some event, such as Page_Load.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Butbtnsaveon1.Visible = AllowUpdate;
}
}

ASP.NET - Using checkbox from the calender control

I am implementing a leave system using the calender control. Something like below :-
Following is the markup :-
<asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender"
ShowGridLines="True">
</asp:Calendar>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="vacation" Text="Vacation" />
<asp:ListItem Value="sick" Text="Sick" />
<asp:ListItem Value="training" Text="Training" />
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
Following is the code-behind :-
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (!e.Day.IsOtherMonth && !e.Day.IsWeekend)
{
CheckBoxList list = new CheckBoxList();
list.Items.Add(new ListItem("Half day"));
list.Items.Add(new ListItem("Full day"));
e.Cell.Controls.Add(list);
}
}
However, I am not able to access the the checkbox values neither in the button_click event nor in the DayRender event? Could anybody help? Is there a better way to implement this?
Because you are not binding any event handler to the CheckBox's also you haven't shown us the java-script you are using for Button click so I assume that you instead wanted to run a Server side code instead of Client side java-script. So instead of using OnClick attribute which is for client event script you should use Click Event which is for server side code

How to use ASP.NET <%= tags in server control attributes?

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.

Using code nuggets for setting controls properties

Why cant i use code nuggets to set a control property??For example a validationgroup of a button or the text property of a label.
<asp:Button ID="btn" runat="server" Text="test" ValidationGroup='<% =TestValidate %>'
<asp:Label ID="lbl" runat="server" Text='<% =Test %>' />
Is there any way to set a controls properties without using codebehind?
You could use data binding:
<asp:Label ID="lbl" runat="server" Text='<%# "Hello World" %>' />
provided that you call DataBind in code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind();
}
}
<%=SomeVar %> uses late binding which behaves like a Response.Write (in Page.PreRender, if I remember correctly). Hence it will not be utilized by the server controls like the way you wanted it. Unless you use code-behind or inline-code-behind to perform binding.

Resources