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 ?

4
  • 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. Commented Jul 3, 2014 at 21:58
  • mmmm, I thought doing this, but I wondered if there is any way to put if statement here Commented Jul 3, 2014 at 22:03
  • Take a look at this question and answer: stackoverflow.com/questions/5596484/…. The Label control has got DataBinding event that You could probably use to handle the logic in the code-behind: msdn.microsoft.com/en-us/library/…. Commented Jul 3, 2014 at 22:07
  • I just answer pretty much the identical question: stackoverflow.com/questions/24568774/… Commented Jul 4, 2014 at 14:59

3 Answers 3

0

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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
     }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.