1

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 ?

user3797485
  • 11
  • 1
  • 2
  • 5
  • This will be painful for you as you go forward. I would recommend doing the conversion in code behind. Also, don't hardcode countries, put them in a database and pull the 'Text' instead of the 'Code' when binding. I.E. instead of binding 'USA', send 'United States' from the beginning. – Kaizen Programmer Jul 03 '14 at 21:58
  • mmmm, I thought doing this, but I wondered if there is any way to put if statement here – user3797485 Jul 03 '14 at 22:03
  • Take a look at this question and answer: http://stackoverflow.com/questions/5596484/asp-net-using-bind-eval-in-aspx-in-if-statement. The `Label` control has got `DataBinding` event that You could probably use to handle the logic in the code-behind: http://msdn.microsoft.com/en-us/library/system.web.ui.control.databinding%28v=vs.110%29.aspx. – Lukasz M Jul 03 '14 at 22:07
  • I just answer pretty much the identical question: http://stackoverflow.com/questions/24568774/how-to-use-if-else-statement-in-databinder-eval-in-asp-net/24576888#24576888 – Kelsey Jul 04 '14 at 14:59

3 Answers3

0

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
     }
  }
}
Dennis R
  • 3,195
  • 1
  • 19
  • 24
0

You need to use find control if thus label is within the gridview and then get the sting from label.

user3729645
  • 147
  • 3
  • 9
0

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

referscus
  • 791
  • 5
  • 11