Is it possible to set up declarative control binding in the codebehind? - asp.net

In ASP.NET WebForms, you can be all like:
<ItemTemplate>
<asp:Label ID="mylabel" Text='<%#Eval("myvalue") %>' runat="server"></asp:Label>
</ItemTemplate>
This declarative binding gets the value from the proper column/member in the datasource. Is it possible to do this in the codebehind:
mylabel.Text = String.Format("<%# Eval(""{0}"") %>", ControlName)
Edit: inb4 have you tried it? Yes. I will add that there's some parsing somewhere that actually prevents that string from showing when my grid binds, so I know I am touching onto something here.

Related

Repeater: databind server-side ID in item control?

When binding a datasource to a Repeater control, is it possible to databind to the ID property of a server-side control inside the ItemTemplate? like so:
<asp:Repeater ID="rptToolTips" runat="server">
<ItemTemplate>
<telerik:RadToolTip ID="tt<%# Eval("Name") %>" ClientIDMode="Static" runat="server">
<div class="tip">
<div class="segName">Segment #<%# Eval("Name") %></div>
<div>Flow:<%# Eval("Flow", "{0:N}") %></div>
</div>
</telerik:RadToolTip>
</ItemTemplate>
</asp:Repeater>
...here I'm trying to set the RadToolTip's ID property to "tt[name-value]". I've tried a few variants but they're invalid:
ID="tt<%# Eval("Name") %>"
ID='tt<%# Eval("Name") %>'
ID="tt<%# Eval('Name') %>"
It's not possible. A server control's ID is set at the time it's being created or at design time. Once it's set you cannot reset it during the data binding.
But, if you are trying to use this ID value in JQuery or JavaScript you could employ the following hack.
Add a custom property to your control (give it any name you like and in this case I'll use myId)
myId='<%# Eval("Name") %>'
No you can find this element by this myId property
Hope this helps.
Try this to assign ID
ID=' "tt" + <%# Eval("Name") %>'
Also see this answer by me to one of the questions at SO for another way (using pure JavaScript's this object rather than using a custom property to get the clientID) of doing this.
Just thought of keeping these linked for future reference.

Using hyperlink with querystring for gridview row

Is there someway to turn the row of a gridview into a hyperlink so that when a user opens it in a new tab for example, it goes to that link? Right now I am using a LinkButton and when the user opens it in a new tab, it doesn't know where to go.
I figured the .aspx code would look something like:
<asp:TemplateField>
<ItemTemplate>
<Hyperlink ID="hyperlink" runat="server" ForeColor="red" HtmlEncode="false" navigationURL="testUrl.aspx"
</ItemTemplate>
</asp:TemplateField>
The only thing is, our URLs are set up in the C# code behind as a query string, so I'm not sure how to pass that into the navigationURL section.
I'm guessing there's something I can do on the page_load with the query string to redirect to the page I need, but this is my first time working with query strings so I'm a little confused.
Thanks!
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#String.Format("~/controller.aspx?routeID1={0}&routeID2={1}", Eval("routeid1"), Eval("routeid2"))%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
routeid1 and routeid2 are passed as query strings to the controller of that page.
What I did recently is modified my class to have a readonly property that constructs the A tag for me. This way I have control over what gets displayed; just text or a link.
<ItemTemplate>
<asp:Label ID="ColumnItem_Title" runat="server" Text='<%# Bind("DownloadATag") %>'> </asp:Label>
</ItemTemplate>
The code behind just binds an instance of the class to the gridview. You can bind the gridview whenever, on load on postback event, etc.
Dim docs As DocViewList = GetViewList()
GridViewDocuments.DataSource = docs
GridViewDocuments.DataBind()
In the above code, the DocViewList, instantiated as docs, is a list of a class that has all the properties that are needed to fill my GridView, which is named GridViewDocuments here. Once you set the DataSource of your GridView, you can bind any of the source's properties to an item.
Something like:
<asp:LinkButton ID="LinkButton_Title" runat="server" target="_blank"
PostBackUrl='<%# Eval(Request.QueryString["title"]) %>'
or binding them from the RowCreated event:
protected void GridView_OnRowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.FindControl("LinkButton_Title") as LinkButton).PostBackUrl = Request.QueryString["title"]))
}
}

Get the this object using eval in a gridview in asp

I can't find any answer anywhere. I want to refer to the row object itself in an databinding expression in a gridview, like this:
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label runat="server"
Text = '<%# GetPendingReason(Eval("this")) %>' />
</ItemTemplate>
</asp:TemplateField>
But it doesn't work because "this" doesn't refer to any attribute. Referencing individual attributes works fine, but how do you refer to the current row?
Simply use <%# Container.DataItem %>. Do not use Databinder.
If you want to refer to the current row you do that at codebehind using
GridViewRow row = GridView1.Rows[index];
at any of the GridView event.

GridView and Eval

I am trying to pass a value through Eval in my GridView, but instead of passing the actual value, it passes the string '<%# Eval etc...This is my code, can anybody advise?
enter code here<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnUpload" OnClientClick="loadDialog('<%# Eval(PK_SpecialEvent).ToString() %>') " Text="Upload/Open Files" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Try this:
OnClientClick='<%# Eval("PK_SpecialEvent", "loadDialog(\"{0}\");") %>'
Another, more readable, way is to do this in codebehind. A good place would be in GridView's RowDataBound Event.
I've only done a GridView once before in my so-far beginners experience of ASP.NET, but should the:-
Eval(PK_SpecialEvent).ToString()
have quotes added in to become like:
Eval("PK_SpecialEvent").ToString()
At least - it does in my working code of a GridView.

Totally lost – data binding expressions inside GridView’s template

1) On aspx page we define GridView control named gvwPolls, and inside its template we define a user control named pollBox1
<asp:GridView ID="GridView1" DataSourceID="objPolls" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
Question is : <%# Eval("QuestionText") %> <br />
<mb:PollBox ID="PollBox1" runat="server" PollID='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="objPolls" ...></asp:ObjectDataSource>
a) I assume that inside gvwPolls’s template, the gvwPollBox1.DataBind is called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?!
b) Can someone offer some explanation how or why is gvwPollBox1.DataBind called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?
2) Continuing with the above example:
-- pollBox1 user control defines a Repeater control named rptOptions:
<asp:Repeater runat="server" ID="rptOptions">
<ItemTemplate>
<%# Eval("pollBoxTitle") %>
</ItemTemplate>
</asp:Repeater>
-- In pollBox1’s code-behind file we bind rptOptions to a data source inside DoBinding() method.
-- We also override pollBox1’s DataBind() method:
public override void DataBind()
{
base.DataBind();
DoBinding();
}
a) I assume that due to overriding pollBox1.DataBind(), the data binding expression <%# Eval("pollBoxTitle") %> ( defined inside rptOptions’s template ) will get evaluated prior to a call to DoBinding method? If so, won’t then <%# Eval("pollBoxTitle") %> get evaluated before rptOptions is actually bound to a data source?
b) If that is the case, how then is rptOptions able to extract value ( from data source’s pollBoxtitle property) from a data source, if at the time the <%# Eval("pollBoxTitle") %>
expression got evaluated, rptOptions wasn’t yet bound to any data source?
thanx
I can't explain why the page life cycle is the way that is, probably has something to do with rendering childs before the parent object. When exactly do you call .DataBind() in the PollBox control? Try to move it into an event that is later in the life cycle, like PreRender.
There is also another way to ensure it is working the way you want to:
Subscribe to the RowDataBound Event, use .FindControl("YourPollBoxID") to get the instance of the control current bound row, set the properties and perform a manuall .DataBind();

Resources