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
Related
can we pass multiple eval field in one command argument.
my code is here
<asp:TemplateField HeaderText="Details" SortExpression="source">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("source") %>' CommandName="Download" Text='<%#Eval("source") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I want to be pass many Eval field in single command argument with command name
if possible please show any reference.
If this is what you are asking as you didnt provide any code snippet i'm assuming like this
CommandArgument='<%#Eval("ScrapId")+","+ Eval("UserId")%>'
In code behind you can use retrieve values like this
protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Comment")
{
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
string scrapid = commandArgs[0];
string uid = commandArgs[1];
}
}
You can do something like
<%# Eval("Param1").ToString() + Eval("Param2").ToString() %>
with asp.net c# I get the code of a country with bind :
<asp:Label runat="server" Text='<%# Bind("h_country") %>' ID="Label1"></asp:Label>
this return code of country like usa, tr, eg, il.
I need to put if conditions and I try to do it like this:
<asp:Label runat="server" Text='<%# if(Bind("h_country")=="usa") resonse.write("United States"); %>' ID="Label1"></asp:Label>
note: I'm using GridView and template.
I tried alo at code behind to get the value of country like this:
String s = Lable1.Text;
but also not workes!
how can I get it as a variable and use if condition ?
You need to use find control if thus label is within the gridview and then get the sting from label.
You probably want to handle this in the code behind
<asp:Label runat="server" Text='<%# GetCountry(Eval("h_country")) %>' ID="Label1"></asp:Label>
code behind
public string GetCountry(object country)
{
if (county.ToString() == "usa")
return "United States";
}
It would be better if you had a lookup so you don't have to have a lot of if statements
This is how you can do conditional checks in the markup while binding. Try something like below but I would not advise doing this kind of conditional checks and response.write at the template level but do it in code behind.
<asp:Label runat="server" Text='<%# Bind("h_country") == "usa" ? "United States" : (string)Bind("h_country") %>' ID="Label1"></asp:Label>
And this is how you can do it at code behind. Use the databound event to find the Label1 and set the appropriate text in there.
You need to define OnRowDataBound="gvTest_DataBound for the gridview in the markup
protected void gvTest_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblCountry = (Label)e.Row.FindControl("Label1");
if (lblCountry.text == "usa"){
// do something here
}
else {
// do something otherwise
}
}
}
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;
}
I am developing a small shopping cart website in asp.net and c#. My problem is in the checkout page I want to display the total price of each product by calculating adding tax to price. I have a datalist which has an ItemTemplate as label for total where I want to bind function call CalculateTotal(price, tax). In database, price has Varchar(MAX) and tax has float datatype. When I tried I got below error:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1502: The best overloaded method match for 'ProgearHire.Hire.CalculateTotal(string, string)' has some invalid arguments
Source Error:
Line 232:
Line 233:
Line 234: ' runat="server" />
Line 235:
Line 236:
The code which I used is below:
<td align="center">
<asp:Label ID="lbTotal" runat="server" Text=""></asp:Label>
<asp:Label Visible="false" ID="Label9" Value='<%#(CalculateTotal(Eval("Price"), Eval("Tax"))) %>' runat="server" />
</td>
Function:
public float CalculateTotal(string x, string y)
{
p = Convert.ToInt32(x);
q = Convert.ToInt32(y);
return (float)p * (float)q;
}
Can anybody help me in find out the problem. Thanks in advance.
dear friend your code is doing well but the lable control have no value property your have to make your lable to text property so make your code like the bellow
<td align="center">
<asp:Label ID="lbTotal" runat="server" Text=""></asp:Label>
<asp:Label Visible="false" ID="Label9" Text='<%#(CalculateTotal(Eval("Price"), Eval("Tax"))) %>' runat="server" />
</td>
Your method CalculateTotal(string x, string y) is expecting two string arguments, but you are supplying objects as the Eval() method returns object. Correct as follows:
... Value='<%# CalculateTotal(Eval("Price") as string, Eval("Tax") as string) %>'
I'd suggest casting each parameter to a string, Eval returns object. As below:
<asp:Label Visible="false" ID="Label9" Value='<%#(CalculateTotal((string)Eval("Price"), (string)Eval("Tax"))) %>' runat="server" />
You have given wrong attribute for label. It would be "Text" instead of "Value"
<asp:Label Visible="false" ID="Label9" Value='<%#(CalculateTotal(Eval("Price"), Eval("Tax"))) %>' runat="server" />
Replace with this
<asp:Label Visible="false" ID="Label9" Text='<%# CalculateTotal(Convert.ToString(Eval("Price")), Convert.ToString(Eval("Tax"))) %>' runat="server" />
Your error will solve. Also, in the Calculate function you have convert the string to int and return as float. This is also wrong as the tax might be in a decimal format. Please do check that thing
Change your function as below.
public Double CalculateTotal(string x, string y)
{
Double p = Convert.ToDouble(x);
Double q = Convert.ToDouble(y);
return (p * q);
}
The code looks great..
<asp:Label Visible="false" ID="Label9" Text='<%# CalculateTotal(Convert.ToString(Eval("Price")), Convert.ToString(Eval("Tax"))) %>' runat="server" />
Try the function like this..
public Double CalculateTotal(object x, object y)
{
Double p = Convert.ToDouble(x);
Double q = Convert.ToDouble(y);
return (p * q);
}
It is working for me. Happy coding.
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