I have constructed an ASP.NET user control "Box.ascx" wtih the following code.
<div id="divContent" runat="server" visible='<%# AllowedToView(this.Privacy) %>'>
Content
</div>
In the codebehind, "Box.ascx.cs" has the following code.
public string Privacy = string.Empty;
public bool AllowedToView(string privacy)
{
return true;
}
When I use this control in a repeater, AllowedToView() function is hit. If I use this control without a repeater, AllowedToView() function isn't called. I want to know why this weird situation happens and how can I make the control call the AllowedToView() function when used without repeater.
Details are below.
I use this control in a repeater in "Default.aspx".
<asp:Repeater ID="rpRecords" runat="server">
<ItemTemplate>
<uc1:Box ID="myBox" runat="server" RecordID = '<%# Eval("RecordID") %>' />
</ItemTemplate>
</asp:Repeater>
The repeater is databound in "Default.aspx.cs" with the following code:
DataTable dt = FillTable();
rpRecords.DataSource = dt;
rpRecords.DataBind();
I use the "Box.ascx" control in "ShowBox.aspx" with the following code.
<body>
<uc1:Box ID="myBox" runat="server" />
</body>
I give values to the user control from the codebehind with the following code.
protected void Page_Load(object sender, EventArgs e)
{
myBox.RecordID = "1";
}
As mentioned in another answer, the # means it will require databinding to be executed.
So to answer your question "How to make it run outside of the repeater" the simple answer is to call myBox.DataBind().
Your question is very similar to asp.net inline code <%# MyboolVal %>. The problem is that <%= is equal to Response.Write and outputs straight HTML, so it won't work when setting the visible property.
I don't think you need the # but instead = in the ASP tag. Pretty sure # is only for databinding events and that's why it works in a repeater because a repeater performs a databound for rendering.
Check this link: http://blogs.msdn.com/b/dancre/archive/2007/02/13/the-difference-between-lt-and-lt-in-asp-net.aspx
Im no expert on webforms but i think that your problem is that you are trying to databind that method and thats not working for you, try putting it in a <%= AllowedToView(this.Privacy) %>
Related
I want to add some asp.net linkbutton controls to a literal on a c# web form application like code below:
StringBuilder sb = new StringBuilder("<ul>");
sb.Append("<li>");
sb.AppendFormat("<asp:LinkButton runat='server' class='add-row' ID='BtnAdd' OnClick='BtnAdd_Click' CommandArgument='test'>{0}</asp:LinkButton>", "Text on the link");
sb.Append("</li></ul>");
this.Literal.Text = sb.ToString();`
and also i want to fire event of click and get CommandArgument's value of that.
private void BtnAdd_Click(object sender, EventArgs e)
{
//get the value of CommandArgument
}
I tried these and the linkbutton added to the literal successfully. but the linkbutton didn't convert to <a> tag and i found that with the firebug like this and no any events couldn't fire:
I want make a tree with leafs as linkbuttons using <ul><li> tags overall.
It would be very helpful if someone could explain solution for this problem.
The correct approach here is via the WebControl api and not from the template.
Your approach may work for HTML content only.
What you need to do is handle a page lifecycle event like OnInit, get a reference to your container control (this.MyPanel), and dynamically add items to that control's children property (this.MyPanel.Children.Add(new....))
The suggested Repeater solution is not suited for tree structure data sources, works for a list though...
As suggested by Andrei using linkbuttons in this way will not work.
I have used a similar solution in the past by creating a query parameter for each a tag in the literal and redirecting to either the current page or a different page and extracting the query parameter from there.
I would however advise you to first try and see if there is not a different way to accomplish what you are trying to do as the literal solution ends up very cluttered and code heavy.
I think a Repeater would better suit your purpose.
<ul>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<li><asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnClick="BtnAdd_Click">Text on the link</asp:LinkButton></li>
</ItemTemplate>
</asp:Repeater>
</ul>
And if you want to use CommandArgument you need an OnCommand, not a OnClick
<asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnCommand="BtnAdd_Command" CommandArgument='<%# Eval("value") %>'>
protected void BtnAdd_Command(object sender, CommandEventArgs e)
{
string commandArgument = e.CommandArgument.ToString();
}
If you have single <asp:LinkButton ..> then you can set it's property at run time. Like
BtnAdd.Id="";
or if you multiple then you can use AddControl method below is link
How to add controls dynamically when click button in asp.net?
How to add controls dynamically to ASP.NET form?
If you have to use asp: LinkButton just for one time then do it in this way.
<asp:Literal runat="server" ID="DocLink"></asp:Literal>
<asp:LinkButton runat="server" ID="LinkButton1" OnCommand='LinkButton1_Click'></asp:LinkButton>
and in .cs file
LinkButton1.Text = CurrentListItemAbsoluteUrl;
LinkButton1.CommandArgument = CurrentListItemAbsoluteUrl;
public void LinkButton1_Click(Object sender, CommandEventArgs e)
{
e. CommandEventArgs //To get your parameter
when I'm doing this, it works just fine:
submitBtt.Text = "test";
in the page load function.
but, when I'm trying to use it like this:
<asp:Button ID="submitBtt" OnClick="submitBtt_Click" runat="server" Text=' <%# test() %>' Width="80px" />
the test function is in the code behind:
public string test()
{
return "test";
}
all i get is a button with no text.
tried to Google it a lot and couldn't find any answer.
Thanks in advance.
<%# is for databinding context only. So you need to call DataBind on the NamingContainer of this control. In this case the page itself:
For example in Page_Load:
this.DataBind();
Here's a quick overview over the inline asp.net tags.
I would prefer using codebehind without the inline aspx since that's confusing and also hard-wiring the view with the business logic.
In an ASP.NET project, I have a literal. In order to set the text property, I used the following code:
<asp:Literal ID="ltUserName" runat="server" Text="<%= session.UserName %>" />
But instead of value of session.UserName, literal shows <%= session.UserName %>. I feel that solution is simple but I couldn't do it. How can set the text with inline code?
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or this.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
<asp:Literal ID="ltUserName" runat="server" Text='<%# Session["UserName"]%>'></asp:Literal>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["UserName"] = "Sample";
this.DataBind();
}
}
If you actually want to print the session value in the HTML page just use
<% =Session["UserName"].ToString()%> as "<% %> will act as server tag and you cant give it inside the literal control
I mean no need of Literal Control can just use mentioned coding instead of literal.
Did you tried:
Text='<%= session.UserName %>'
Single quotes may solve it
EDIT:
Based on this thread: stackoverflow.com/a/370263/360171
I would simply replace
<asp:Literal ID="ltUserName" runat="server" Text="<%= session.UserName %>" />
by
<%= session.UserName %>
You can't mix controls (<asp:Literal />) with code blocks (<%= .. %>).
You can access the Text property from within code:
ltUserName.Text = session.UserName;
Renatos answer is correct, you should put single quotes when you are going to evaluate an expression in a property.
The same can be said with a ItemTemplate, where you have controls to databind, where you would use Text='<%=Eval("MyDataProperty")%>'.
i have this code in an itemtemplate in a gridview:
<%# DataBinder.Eval (Container.DataItem, "DiscountAmount")%>
It's a decimal value, and it shows 20.300000000000, which is technically right, but i'd prefer to show 20.30 or 20,30, depending on the culture.
But i've never been a big fan of gridviews, and the DataBinder.Eval and Container.DataItem haven't been good friends either, and i'm lost with how to use it.
it has a special prefix (<%#) and when i type anything other then the original code it's no good, but changing <%# to <%= or <% doesn't seem to work either?
This will also work:
<%#= String.Format("{0, 0:N2}",DataBinder.Eval (Container.DataItem, "DiscountAmount"))%>
Edit: I share your discomfort with declarative databinding syntax. You can accomplish the same thing in code-behind by calling the RowDataBound event and implementing whatever changes you want to make as the data is bound to the GridViewRow.
To do this, you need to wire up the event in the markup by setting OnRowDataBound to the name of your event handler, something like this:
<asp:GridView ID="InvoiceGrid" OnRowDataBound="InvoiceGrid_RowDataBound".....>
Then you create an event handler in code behind with a signature like this:
protected void InvoiceGrid_RowDataBound(object sender, GridViewRowEventArgs e)
The first thing you do in that event handler is test which type of GridViewRow type it is:
if (e.Row.RowType == DataControlRowType.DataRow)....
Then you do whatever formatting you want to do.
For folks happy with declarative markup, this may seem burdensome. But for people who are comfortable writing code, you can do a whole lot more here in code behind.
Did you try this?
<%= String.Format("{0:0,0.00}", DataBinder.Eval (Container.DataItem, "DiscountAmount"))%>
or just
<%# DataBinder.Eval(Container.DataItem, "DiscountAmount", "{0:0,0.00}")
You can read more format options in the article String Format for Double.
There are several ways...some of them stated above and here is another one:
Text='<%# GetFormattedDiscount(Eval("DiscountAmount").ToString())%>'
GetFormattedDiscout is a function in your code-behind where you can do whatever formatting you need and return it as string:
protected void GetFormattedDiscount(string amount){
return String.Format("{0:N2}",amount);
}
Even this should work:
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#String.Format("{0:n2}",Eval("DiscountAmount")) %>'></asp:Label>
</ItemTemplate>
I came across a very strange behavior in asp.net's ObjectDataSource, the description to reproduce is somewhat long, so bear with me while I set the scene.
So, imagine a trivial ObjectDataSource/GridView combo in a User Control. The ObjectDataSource calls a method which returns a List of objects, and the GridView shows these objects in tabular form:
<div runat="server" ID="ControlWrapper">
<asp:GridView ID="GridView1" AutoGenerateColumns="true" DataSourceID="ObjDataSource1" OnRowDataBound="GridView1_RowBound" runat="server">
</asp:GridView>
</div>
<asp:ObjectDataSource ID="ObjDataSource1" runat="server" SelectMethod="GetBundle" OnSelecting="FixDataSource_Selecting" OnSelected="FixDataSource_Selected"
TypeName="West.VitalSigns.Contracts.ProdFixController">
</asp:ObjectDataSource>
This approach will work with pretty much nothing in the code-behind. But let's say that we want to create n number of GridView-s depending on the contents of the database. So we comment out the GridView in the markup...
<div runat="server" ID="ControlWrapper">
<!--
<asp:GridView ID="GridView1" AutoGenerateColumns="true" DataSourceID="ObjDataSource1" OnRowDataBound="GridView1_RowBound" runat="server">
</asp:GridView>
-->
</div>
...and add something like this to the ObjectDataSource's Selected event handler:
protected void FixDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs args)
{
HashSet<string> components = new HashSet<string<()
foreach (ProdFix fix in (List<ProdFix>)args.ReturnValue)
{
if (!components.Contains(fix.Component))
{
GridView v = new GridView();
v.ID=fix.Component.Replace(" " ,"").Replace("-","");
v.AutoGenerateColumns = true;
v.DataSource = args.ReturnValue;
v.RowDataBound +=new GridViewRowEventHandler(BundleGrid_RowBound);
ControlWrapper.Controls.Add(v);
components.Add(fix.Component);
}
}
}
This code works (or at least the un-simplified version works on my machine), so you decide to remove the commented-out section from the markup (don't want that cruft hanging around, after all!)
<div runat="server" ID="ControlWrapper">
</div>
When you do this, however, the code no longer works! The ObjectDataSource won't fire, which means that the Selected event will never happen, which means you won't get your GridView-s. It looks like ObjectDataSource is reacting to commented-out markup in the aspx file?
So, is this:
A bug in ASP.NET?
A non-standard way of dynamically creating GridViews?
A WTF that I shouldn't have tried anyway?
All of the above?
Your gridview control in the markup is not hidden, even with the comments. HTML comments do not apply to server-side tags. Use server side comments instead:
<% /* %> <% */ %>
or
<%-– and -–%>
24hrs later, I noticed that this approach to getting N number of grid-views was pretty silly. Instead of using an ObjectDataSource I refactored my code to just call the GetBundle method directly from Page_Load() and create the GridViews programatically.
cdonner has the answer correct about the server-side comments. I didn't realize that there was a difference.
This should work too, since you're "disabling" the tag with it:
<!--asp:GridView ID="GridView1" AutoGenerateColumns="true" DataSourceID="ObjDataSource1" OnRowDataBound="GridView1_RowBound" runat="server">
</asp:GridView-->