Put variables and statements into text of a button - c#

I'm trying to figure out how to put something else than just static string into text attribute of <asp:button>. Following code:
<asp:Button runat="server" ID="updateList" Text=<%= isEditing ? Resources.Labels.Update : Resources.Labels.Insert %> />
returns
Parser Error Message: Server tags cannot contain <% ... %> constructs.

You need a Databinding expression for that <%# %>:
<asp:Button runat="server" ID="updateList" Text='<%# isEditing ? Resources.Labels.Update : Resources.Labels.Insert %>' />
But for it to work outside a GridView, Repeater etc you have to call DataBind() manually.
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}

Related

Pass variable value from aspx link button to code behind in ASP. NET using C#

Inside a for loop, how to pass the variable to the code-behind from an asp tag and access the variable value
CommandArgument works but shows <# gigs[x].Id%> - not the value.
Aspx
<%for (int x = 0; x < gigs.Count; x++){%>
<asp:LinkButton ID="LinkButton2" CssClass="btn btn-danger mt-3" runat="server"
UseSubmitBehavior="false" CommandArgument="<# gigs[x].Id%>"
OnClick="LinkButton2_Click">Hide Gig <i class="fa fa-eye-slash ml-1"></i>
</asp:LinkButton>
Code behind
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
String Value1 = lnk.CommandArgument;
Response.Write(Value1);
}
Result
<# gigs[x].Id%>
Expected Result
1
Try using <%# gigs[x].Id %> instead and call Page.DataBind() on the Page_Load event.
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.
<%# %> can be used, only if the container is databound (the page in your case).
You're much better off using a Strongly Typed Repeater. You can set the ItemType as your class gigs and have full access to all it's properties. Then you can easily use them in the LinkButton.
Repeater1.DataSource = gigs;
Repeater1.DataBind();
And then in the aspx
<asp:Repeater ID="Repeater1" runat="server" ItemType="MyNameSpace.MyClass.Gig">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" CommandArgument='<%# Item.Id %>' OnClick="LinkButton2_Click" runat="server">Hide Gig</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

How to change the layout of QueryableFilterRepeater?

I am using an ASP.NET Dynamic Data Entities web application which contains a QueryableFilterRepeater server control inside the content placeholder on page List.aspx, when I execute the website, the filter control shows all the filters (Label Name along with the corresponding Drop Down) vertically.
Is there any way we can change the layout and display the filters horizontally?
Please find the .aspx code below
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged" />
</ItemTemplate>
</asp:QueryableFilterRepeater>
The corresponding .cs file code:-
protected void Label_PreRender(object sender, EventArgs e)
{
Label label = (Label)sender;
DynamicFilter dynamicFilter = (DynamicFilter)label.FindControl("DynamicFilter");
QueryableFilterUserControl fuc = dynamicFilter.FilterTemplate as QueryableFilterUserControl;
if (fuc != null && fuc.FilterControl != null)
{
label.AssociatedControlID = fuc.FilterControl.GetUniqueIDRelativeTo(label);
}
}
protected void DynamicFilter_FilterChanged(object sender, EventArgs e)
{
GridView1.PageIndex = 0;
}
Need suggestions.
Thanks in advance.
I usually wrap the control in a span element and then use CSS to style it the way that i want:
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<span class="filter">
<asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged"/>
</span>
</ItemTemplate>

Dynamically set text of Radtooltipmanager

This is my .aspx code:
<asp:LinkButton ID="link" runat="server"> <telerik:RadToolTipManager
ID="toottip" runat="server" AutoTooltipify="true" Width="200px"
RelativeTo="Element" HideEvent="LeaveTargetAndToolTip"
WebServiceSettings-UseHttpGet="false" Animation="Fade"
EnableTheming="true" Title="Documents" ShowEvent="OnMouseOver">
<WebServiceSettings Method="GetToolTipData"
Path="InvestmentDropDownWebService.asmx" UseHttpGet="true"/>
<TargetControls>
<telerik:ToolTipTargetControl TargetControlID="link"></telerik:ToolTipTargetControl>
</TargetControls> </telerik:RadToolTipManager> </asp:LinkButton>
And this is the code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = getName();
link.Text = dt.Rows[0][Name].ToString();
}
I am trying this code but the problem is that on mouseover, the webservice is not called. If I set link button Text in .aspx page it's working fine...
How to solve it if the text is from codebehind?
did you tried by using by giving empty text in aspx page like
<asp:LinkButton ID="link" runat="server" Text=" "> <telerik:RadToolTipManager
ID="toottip" runat="server" AutoTooltipify="true" Width="200px"
RelativeTo="Element" HideEvent="LeaveTargetAndToolTip"
WebServiceSettings-UseHttpGet="false" Animation="Fade"
EnableTheming="true" Title="Documents" ShowEvent="OnMouseOver">
<WebServiceSettings Method="GetToolTipData"
Path="InvestmentDropDownWebService.asmx" UseHttpGet="true"/>
<TargetControls>
<telerik:ToolTipTargetControl TargetControlID="link"></telerik:ToolTipTargetControl>
</TargetControls> </telerik:RadToolTipManager> </asp:LinkButton>

Passing variables to usercontrol inside the listview (asp.net, c#)

I want to pass some dynamic information from the listview to UserControl, but I guess I'm missing something.
.aspx page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource"
DataKeyNames="id_Image">
<ItemTemplate>
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description")%>' ID="info1" runat="server" />
</ItemTemplate>
</asp:ListView>
.ascx file:
Name:
<asp:Label ID="NameLabel" runat="server" />
Description:
<asp:Label ID="DescriptionLabel" runat="server" />
.ascx codebehind file:
public string Name_Lbl { get; set; }
public string Description_Lbl { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Text = Name_Lbl;
DescriptionLabel.Text = Description_Lbl;
}
Everything is working fine if Im trying to get value from string text like:
<uc1:Info Name_Lbl="Name" Description_Lbl="Description" ID="info1" runat="server" />
But when I'm trying to get value from Datasource, the string values in usercontrol are "null"
Can anyone see what I'm doing wrong? Thanks, Jim Oak
DataBinding occurs a lot later in the Control Life cycle than Load.
You assign your text on Load, but your control only receives the text on DataBind
To fix this you can set your text OnPreRender. This occurs after DataBind
protected override void OnPreRender(EventArgs e)
{
NameLabel.Text = Name_Lbl;
DescriptionLabel.Text = Description_Lbl;
}
Everything looks fine in your code just check the code line:
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description"%>' ID="info1" runat="server" />
You are missing the closing bracket ")" against Description_Lbl. It should be:
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description")%>' ID="info1" runat="server" />

Why Link Button control variable doesn't get any value?

I created LinkButton that located inside of Repeater Control.
CategoryID is a variable in LinkButton Control that have to get value after Repeater Control was bound to data. But CategoryID always get zero.
I have the following ASP and C# code:
<asp:Repeater ID="rpt1" runat="server"
OnItemDataBound="rpt1_ItemDataBound"
OnItemCommand="rpt1_ItemCommand">
<ItemTemplate>
<div>
<%# Eval("Name") %>-<%# Eval("CollectionType")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="[edit item]"
PostBackUrl='AddItem.aspx?CategoryID=<%# Eval("CollectionID")%>' />
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<GlassesCollection> gc = BL.GetDataBL.GetCollection();
rpt1.DataSource = gc;
rpt1.DataBind();
}
}
Any idea why CategoryID variable doesn't get any value and how can I fix the problem?
A server control parameter cannot contain a mixture of literal text and evaluated expressions.
The code you have will literally be posting back to AddItem.aspx?CategoryID=<%# Eval("CollectionID")%> and it will not be evaluating the code within the angle brackets.
You need to change your parameter like so
PostBackUrl='<%# "AddItem.aspx?CategoryID=" + Eval("CollectionID")%>' />

Categories

Resources