1

I am using VS 2019, C# 4.8 and ASP.NET WebForms. I have a page that serves as a listing. Each row has a company name and when the user clicks on it, I want top another page and pass that company's Id in as a parameter. Or is there a better way to do this that's SEO friendly?

Below is a screenshot of my troubled code:

enter image description here

  1. As you can see, I'm trying to pass the company ID into the url as a parameter (itm.Co_Id) but this fails as shown in the next screenshot:

enter image description here

  1. If I put the row# and company name in the anchor tag's text area it won't compile as shown below:

enter image description here

What is the right way to do this?

1 Answer 1

1

Unless you are inside of a data bound control, then you can't use

'<% Public Method of form goes here %>'

The above will not work. You CAN use the above expression in markup and controls that are NOT server based (so, you could remove runat="Server".). So any server control?

Those <% %> expressions can't be used!

Of course if we are inside of say any databound control? Then sure, you can use the "#" sign that means "during" data binding.

So, if this is say in side of a listview/gridview/details view etc.? And that item is data bound?

Then you CAN use this format in your markup:

'<%# Eval("column name from data source") %>'

'<%# Eval("HotelName") %>'

'<%# Eval("itm.Co_ID") %>'

But above assumes that these controls are inside of a database control (gridview/listview etc.).

So, this would be legal assume that itm.Co_ID is a PUBLIC member of that web form.

<h2>'<%= itm.Co_ID %>'</h2>

In above, itm.Co_ID has to be a public method/function of that given web page.

However, if you try this, then it will NOT work:

<h2 id="mybigtitle" runat="server" >'<%= itm.Co_ID %>'</h2>

So such expressions ONLY work in non server controls (without runat="server').

However, we could execute a databind on "mybigtitle" in code, and thus we could use this:

<h2 id="mybigtitle" runat="server" >'<%# Eval("itm.Co_ID") %>'</h2>

But, in code behind we would then need a

 mybigitle.dataBind()

And item.Co_ID would have to be a resolvable variable, class or public method or function of that web page for this to work.

However, you might well be already inside of a listview or some data bound control that is creating and spitting out the data - without that markup or known what kind of object your posted markup is part of - then it not clear if you can use "#" for a data bind expression such as

'<%# Eval("legal function etc. goes here") %>'

Without knowing if your markup is the result of a databind(able) control, then we have to guess a bit here as to what will work, or not.

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

4 Comments

Thank you Albert for your details answer. I can see where I needed to provide more context. This is not inside a control. I'm creating a listing of companies where the codebhind has a public property which is a list of company business objects. In the page I loop through the list like this building out the needed html like this: <% foreach (var itm in Companies) { %> <div class="row"> <div class="rowContainer"> etc... and this code works when placed inside a div tag: <%= itm.RowNum %>.<%= itm.Co_Name %> So knowing this, how an I create the anchors as needed? Thank you
Yes, but if you make that "div" or things runat=server, then the replace of those expressions does not occur. You have to add a databind() for that runat=server control if you want that expresison to work and you use '<%# Eval("itm.Co_Name") %>' assuming that item.Co_Name is a exposed public member in code behind on that web page.
I still don't understand how to do this. "itm.Co_Name" cannot be a public member in the code behind because "itm" is an item in the list "Companies" that I'm looping through like this: <% foreach (var itm in Companies) { %> ... gen lot so html here includijng the anchor tag we're talking about... the list is the public member. so the question remains, how do I pull data like "itm.Co_Name" into the anchor tag.
Like I said - without you posting more of your markup, we have ZERO CLUE as to where and how you getting that value from - only now do we even know that you running a loop as opposed to a databound control such as dataview, gridview, listview (insert many more here) that are data bound. Using some type of databound control would save you all that looping, and probably world poverty. But, I suppose you can do things old way time wise and write looping code. You have to post more of your markup for us to do more then guess as to how/when/where the context of the expression is you want to use.

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.