Using markup in label text or similar element - c#

I have
<asp:Label ID="lbl_ReadOnlyFld" runat="server"></asp:Label> <%=GetGuiLocalString("lbl_ReadOnlyFldDescr")%>
I need the text in some element so I can access it:
For example:
<asp:Label ID="lbl_InputFld" runat="server"></asp:Label><asp:Label ID="lbl_InputFldDescr" runat="server" text=' <%= GetGuiLocalString("lbl_InputFldDescr")%>'></asp:Label>
It just gives me stuff inside ''... any help would be appreciated.
Regards.

<%= is meant to directly output something on the page and can't be used inside other controls.
You should either use <%# and databind the control or set the text in the code behind. Also the other stuff should be inside those tags so it would be:
<asp:Label ID="lbl_InputFldDescr" runat="server" text='<%# " " + GetGuiLocalString("lbl_InputFldDescr")%>' />
and then lbl_InputFldDescr.DataBind(); somewhere in your code behind (provided you don't already databind the page or someting).

ASPX:
<asp:Label ID="lbl_InputFld" runat="server">my name is Jhon</asp:Label>
<asp:Label ID="lbl_InputFldDescr" runat="server" text='<%# " " + GetGuiLocalString("lbl_InputFldDescr")%>'></asp:Label>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
public string GetGuiLocalString(string id)
{
string s = "hello";
Label lbl = (Label)Form.FindControl(id);
if(lbl!=null)
{
if ( ! string.IsNullOrEmpty(lbl.Text))
s = lbl.Text;
}
return s;
}

Related

C# - Transfer the text of a hyperlink through a session inside a repeater

I want to transfer the text of a hyperlink through a session inside a repeater. But I can not think of the logic to do this. My repeater generates links within that dropdown menu and I want to get the text that appears in the link and pass it to another page Sectors.aspx. This is what I have done so far:
--- edit ---
I want that when the link is clicked, the text that is in the link is passed to the other page. For example, the menu has two links, APPLE and BANANA. When the user clicks APPLE, I want the next page Sectors.aspx to know that the user clicked APPLE and not the other options.
ASPX Page:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="dropdown">
<button class="dropbtn">Setor</button>
<div class="dropdown_content">
<asp:Repeater ID="sectors_menu" runat="server">
<ItemTemplate>
<asp:HyperLink id="hyperlink1" NavigateUrl="Sectors.aspx" Text='<%#((System.Data.DataRowView)Container.DataItem)["sector"] %>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</asp:Content>
Code Behind
public partial class _Default : System.Web.UI.Page
{
MySqlConnection mysql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
string mysql_string;
MySqlDataAdapter mysql_data_adapter;
public void Page_Load(object sender, EventArgs e)
{
Sector_label();
}
public void Sector_label()
{
mysql_string = "SELECT * FROM employees GROUP BY sector";
mysql_data_adapter = new MySqlDataAdapter(mysql_string, mysql_connection);
DataTable data_table = new DataTable();
mysql_data_adapter.Fill(data_table);
DataView data_view = new DataView(data_table);
sectors_menu.DataSource = data_view;
sectors_menu.DataBind();
for (int count = 0; count < sectors_menu.Items.Count; count++)
{
var test = (HyperLink)sectors_menu.Items[count].FindControl("hyperlink1");
Session["session_hyperlink"] = test.Text;
// Debug.WriteLine(test.Text);
}
}
}
You could add the text as a parameter of the NavigateUrl:
<ItemTemplate>
<asp:HyperLink id="hyperlink1" NavigateUrl='<%# "Sectors.aspx?sector=" + Server.UrlEncode(((System.Data.DataRowView)Container.DataItem)["sector"]).ToString() %>' Text='<%#((System.Data.DataRowView)Container.DataItem)["sector"] %>' runat="server"></asp:HyperLink>
</ItemTemplate>
Then, Sectors.aspx can retrieve it through the "sector" parameter:
protected void Page_Load(object sender, EventArgs e)
{
string sector = Request.Params["sector"];
}
That id="hyperlink1" parameter looks fishy, though. You don't want all your hyperlinks to have the same ID.
Just replace hyperlink tag with this code ,
<asp:HyperLink runat="server" Navigateurl='<%#"Sectors.aspx?mySector="+ Eval("sector") %>'
Text='<%#((System.Data.DataRowView)Container.DataItem)["sector"] %>' />

Haw to pass an id to server side method in asp.net

I have a gridview and I want to keep the headers as hyperlink to navigate the link in new tab
<asp:TemplateField
<HeaderTemplate>
<asp:HyperLink ID="hlnk" runat="server" Target="_blank" NavigateUrl='<%# GettheNavigateUrl()%>'>Header</asp:HyperLink>
</HeaderTemplate>
...........
protected string GettheNavigateUrl()
{
return "http://www.google.com/";
}
Now I want to pass the id to the method. How to do it '<%# GettheNavigateUrl(??)%>'
This will work with Jquery
NavigateUrl='<%# GettheNavigateUrl($(this).attr('id'))%>'>
Jquery is Third Party Library which helps you optimize js Code
or
Have a look at this link
http://weblogs.asp.net/gurusarkar/pass-querystring-parameter-with-navigaterurl-in-hyperlink-inside-a-gridview
try this:
in .aspx
<asp:HyperLink ID="hlnk" runat="server" Target="_blank" >Header</asp:HyperLink>
You can add RowDataBound method as GridView_RowDataBound
OnRowDataBound="GridView_RowDataBound"
in code
protected string GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
HyperLink hyp = (HyperLink)e.Row.FindControl("hyp");
hyp.NavigateUrl = "www.google.com" + hyp.ClientID;
// hyp.ClientID is your id of control
}
}

Bind SC Images in Sitecore

I'm having a repeater control and want to bind Images to the sc control for onitemdatabound event.
My markup is:
<sc:Link runat="server" ID="sclnk" Field="#" rel="iframe-960-540">
<sc:image id="scimage" runat="Server" field="#">
</sc:image>
</sc:Link>
And my code is:
Sitecore.Web.UI.WebControls.Link scBannerLink = e.Item.FindControl("sclnk") as Sitecore.Web.UI.WebControls.Link;
if (scBannerLink != null)
{
scBannerLink.DataBind(promoItem.ID.ToString(), promoItem.PromoLink.Field.InnerField.Name);
}
Sitecore.Web.UI.WebControls.Image scPromoImage = e.Item.FindControl("scimage") as Sitecore.Web.UI.WebControls.Image;
if (scPromoImage != null)
{
scPromoImage.DataBind(promoItem.ID.ToString(), promoItem.PromoImage.Field.InnerField.Name);
}
I'm not getting any error but not diaplaying images
I've never used the Databind method of the control to set the properties.
The easier solution is to specify the Fieldname and set the Item in your repeater:
<asp:Repeater runat="server" ID="rptImages">
<ItemTemplate>
<sc:Link runat="server" ID="scLnk" Field="MyLinkFieldName" Item="<%# Container.DataItem %>" Parameters="rel=iframe-960-540">
<sc:image id="scImage" runat="Server" Field="MyImageFieldName" Item="<%# Container.DataItem %>" />
</sc:Link>
</ItemTemplate>
</asp:Repeater>
And you can pass in the additional attributes in the Parameters field on the control as a URL encoded parameters string, e.g. Parameters="rel=iframe-960-540&param2=value2&param3=value3"
And your code behind binding the control should be:
protected void Page_Load(object sender, EventArgs e)
{
rptImages.DataSource = Sitecore.Context.Item.GetChildren(); // this needs to be changed to whatever your query is...
rptImages.DataBind();
}

Aspx C#: How to get result of Eval(...) in grid_HtmlRowCreated?

I'm trying to use a parameter from .aspx in C# code inside grid_HtmlRowCreated method.
I tried it by setting a text in a label in .aspx and then fetch that text from the label.
It worked:
aspx
<dx:ASPxLabel runat="server" ID="cl" Text="Some text"/>
...
protected void grid_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
{
if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
{
...
string id;
if (comments_grid != null && expIDControl != null)
{
id = ((DevExpress.Web.ASPxEditors.ASPxLabel)expIDControl).Text;
System.Windows.Forms.MessageBox.Show( id );
}
}
But when I set the value like:
<dx:ASPxLabel runat="server" ID="cl" Text='<%# Eval("Id") %>'/>
then id is empty and when I set
<dx:ASPxLabel runat="server" ID="cl" Text='<%# Eval("Id") %> + " test"'/>
then id is literally <%# Eval("Id") %> test
Thank You for any ideas...
EDIT: That works as well, the code just need to be in HtmlRowPrepared, not in HtmlRowCreated. Thanks guys!
You can write a method on the class (.aspx.cs) that will concatenate.
Going from memory:
Text='<%# ConcatSomething (Eval("Id") , "Peanut" ) %> '/>
and then
public object ConcatSomething(object x , object y)
{
return Convert.ToString(x) + Convert.ToString(y);
}
or maybe
public object ConcatSomething(string x , string y)
{
return x + y;
}
You'll have to play with it. I've done it a thousand times on data_binding.
The stinker is the "object" stuff.
And you'll have to code for null values (coming in) if you may have some.
I just copied this from my current project , I have the same Grid with Item template and the correct way to do it is :
<dx:ASPxLabel runat="server" ID="cl" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %>' />
regards

How do I run an if statement in aspx?

I would like to run an if statement but the condition uses a variable from the code behind. How do I call that variable? Side note... I am using a gridview and the variable is in a dataset (dsResult - idnbr colum)
<ItemTemplate>
<% string temp = (Eval("idnbr").ToString());
if (temp.Contains("X")) { %>
<asp:Label ID="Label1" runat="server" Text='<%# (Eval("old_amt").ToString(),"ccTot") %>'></asp:Label>
<% } else { %>
<asp:Label ID="Label2" runat="server" Text='<%# (Eval("new_amt").ToString(),"ccTot") %>'></asp:Label>
<% } %>
</ItemTemplate>
Create c# side method that does it for you:
Than use one of 2 ways:
If you deal with well known entity (saying when GridView bound to ObjectDatasource) you
can just cast it to your entity and pass back:
C#:
protected String MySelectorImpl(Object rowData)
{
MyEntity ent = (MyEntity)rowData;
if(ent.idndr .... )
return ....
else
return ...
}
ASP.Net:
<ItemTemplate>
<asp:Label Text='<%# MySelector(Container.DatatItem) %>' ...
Second case - just use eval syntax
C#:
protected string MySelector(Object condition, Object value1, Object value2)
{
if((String)condition ....) return value1.ToString ....
}
ASP.Net:
<ItemTemplate>
<asp:Label Text='<%# MySelector(Container.DatatItem("idnbr", ... %>' ...
(,
I know this doesn't completely answer your question but why not just do this in the code behind? I'm assuming you're doing something with DataBinding?
string temp = (string)DataBinder.Eval(e.Item.DataItem, "idnbr");
string newAmount = (string)DataBinder.Eval(e.Item.DataItem, "new_amt");
string oldAmount = (string)DataBinder.Eval(e.Item.DataItem, "old_amt");
Label lbl1 = e.Item.FindControl("label1") as Label;
if(temp.Contains("X") {
lbl1.Text = oldAmount;
} else {
lbl1.Text = newAmount;
}
You can read from a property that is declared in the code behind; does this satisfy what you want?
Instead of string temp = ..., you can use this.MyProperty.Contains("X")...
<a href="javascript:onclick= window.location = 'RenewalPaymentGateway.aspx?RPID=<%# Eval("RPID")%>'" title="Pay">
<asp:Label ID="TEMP" Text='<%# If(Eval("PaymentStatus").ToString() = "Paid", "View", "Make payment") %>' runat="server" />
here in label the text view will appear when PaymentStatus=paid or the text will be make payment

Categories