ASP:LinkButton and Eval - asp.net

I'm using an ASP:LinkButton inside of an ItemTemplate inside of a TemplateField in a GridView. For the command argument for the link button I want to pass the ID of the row from the datasource that the gridview is bound to, so I'm doing something like this:
<asp:LinkButton ID="viewLogButton" CommandName="viewLog" CommandArgument="<%#Eval("ID")%>" Text="View Log" runat="server"/>
Unfortunately, the resulting HTML is this:
<asp:LinkButton ID="viewLogButton" CommandName="viewLog" CommandArgument="3" Text="View Log" runat="server"/>
It seems that it is parsing the Eval() properly, but this is somehow causing it not to parse the LinkButton tag and just dump it out as literal text. Does anyone know:
a) why this is happening and,
b) what a good solution to this problem is?

While it may not be causing it, I usually define it like this:
CommandArgument='<%#Eval("ID")%>'
Please post the rest of the GridView's markup, as it shouldn't be doing that.

Related

asp:hyperlink href won't show up even though i populate navigateurl

Does anyone know what is wrong with the following two lines of code? In both cases, there is no href in the anchor links when i view it in the browser:
<asp:HyperLink runat="server" NavigateUrl='<%# Eval(Request.QueryString["conferenceId"], "~/Cms/schedule-edit.aspx?conferenceId={0}&type=workshopStream") %>' Text="Create Workshop Stream"></asp:HyperLink>
<asp:HyperLink runat="server" NavigateUrl='<%# String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem", Request.QueryString["conferenceId"]) %>' Text="Create Schedule Item"></asp:HyperLink>
This exact same code seems to work fine when I put it into the ItemTemplate of a Listview though. But it doesn't work when used on it's own in an aspx file.
What's wrong with it?
Also if i replace the navigateUrl with a hardcoded string ~/cms/schedule-edit.aspx?conferenceId=2&type=stuff then the href shows up. It just doesn't work when i have the Eval or String.Format in there.
If those HyperLink server controls are located outside of DataBound controls like GridView, there are two problems in our code -
You want to use <%= %> instead of <%# %> which is used in DataBound controls.
You cannot use <%= %> to set property of a server control. Basically, you cannot mix runat="server" with <%= %>.
Solution
<a href='<%= String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem",
Request.QueryString["conferenceId"]) %>'>Create Workshop Stream</a>
The anchor syntax NavigateUrl='<%#...%> is only valid inside a GridView, ListView, etc. When it is not inside such controls, you can set its NavigateUrl property via code. Obviously, you also need to give an ID to your HyperLink.
The markup:
<asp:HyperLink ID="HyperLink1" runat="server" Text="Create Schedule Item"></asp:HyperLink>
The code behind:
HyperLink1.NavigateUrl = String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem", Request.QueryString["conferenceId"])
You are using a data-binding expression here. This is denoted in the following syntax:
<%# [code] %>
The code inside is evaluated only when the containing control, or any of its ancestors have their .DataBind() method called.
To work this around, you can:
Call Page.DataBind()
This could have some unwanted consequences if you have other data-bound controls on the page, as this method will cause all of them to have their data-binding events fired. Usually, this approach is applied if you have minimalistic code-behind and the entire page relies on data-binding expressions.
Give each HyperLink an ID and call HyperLinkID.DataBind();
Stick to the approach in codingstill's answer by setting the NavigateUrl property in the code behind of your page/user control.

Render hyperlink button through gridview

I have a hyperlink button in gridview with the attribute navigateurl = "".
<asp:HyperLink ID="btntest" runat="server" CommandName="Print"
CommandArgument='<%# Eval("rentalid") %>' NavigateUrl="" CssClass ="ImClass orangeButton"
Text="Display/Print Rental Agreement As PDF111" Target="_blank">
</asp:HyperLink>
I have a gridview command that makes a file and give this hyperlink button navigateurl attribute value but the problem is the hyperlink button is not calling gridview command because navigateurl is blank and also how can I render the hyperlink button when it got the navigateurl at the same time?
Use link button instead and do whatever stuff you want to do in command event of a linkbutton.
you can also style a linkbutton like a button.
I had a similar issue.... there are a few ways we came up with, but the best option for us was to use a literal in our grid-view. We set the html in the literal to open in a new window and passed the query strings that we needed. We did this twice, once selecting out the HTML in a stored proc. The other on a Row Data Bound. It sounds like either could work in your situation.
Hope this helps!

Read Only TextBox Value Disappears on postback

I want to keep TextBox attribute Read Only in web form. But when I keep this Read Only , On PostBackits values get vanishes and I get empty text. How can I achieve the same functionality without loosing TextBox values.
You can achieve this by preventing user to enter values in the textbox, thus add following attributes to the textbox. It will maintain values even on postback also.
onkeypress="return false;"
Change it by removing the ReadOnly=”true” from the tag , we will add it in the code.
Now in the code add the following :
TextBox1.Attributes.Add(“readonly”, “readonly”);
You probably have it like this right now:
<asp:TextBox ID="MyTextBox" runat="server" Enabled="false" />
Just change this to:
<asp:TextBox ID="MyTextBox" runat="server" ReadOnly="true" />
And it will send its value over postback like all other form elements while still being read only.

Passing additional arguments into the OnClick event handler of a LinkButton using Javascript

I have a ASP.NET Website, where, in a GridView item template, automatically populated by a LinqDataSource, there is a LinkButton defined as follows:
<asp:LinkButton ID="RemoveLinkButton" runat="server" CommandName="Remove"
CommandArgument='<%# DataBinder.GetPropertyValue(GetDataItem(), "Id")%>'
OnCommand="removeVeto_OnClick"
OnClientClick='return confirm("Are you sure?");'
Text="Remove Entry" />
This works fine. Whenever the Button is Clicked, a confirmation dialog is displayed.
What I am trying to do now, is to allow the user to enter a reason for the removal, and pass this on the the OnClick event handler. How would I do this?
I tried OnClientClick='return prompt("Enter your reason!");', but, of course, that did not work =)
Personally, I would stash the reason into a hidden field.
It would work something along these lines: your OnClientClick method would take the return value of a JS method, which does the prompt, and then places the result of the prompt into the hidden field.
You can also look into calling __doPostBack from your client-side code instead of using the OnClick postback. Then you can capture the reason and pass it server-side.

asp.net button use javascript return function

I have an asp:button control and am using the "CommandName" and "CommandArgument" parameters in the .aspx file. The CommandName is found with <%# Eval("Name") %>, but the CommandArgument has to be found via a javascript function. However, I'm not quite sure how to do this inline. The function returns a number (integer), which is to be used as the value of the CommandArgument. Is it possible to do this?
Thanks
EDITED TO ADD CODE
I've added an example code (don't have access to the real code atm). However, basically, the CommandArgument should be the value returned by the function CalculateLength().
function CalculateLength(a,b) {
return a*b;
}
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName=<%# Eval("Name") %> CommandArgument= ??? //Should be result of CalculateLength(a,b).
I am not sure how you are going to get the javascript to work because the aspx code is running server side and building the output for your button. By the time the javascript runs the page code has already been built and the button's html and attached javascript as well.
Is there anyway to calculate the function server side and then just do:
CommandArgument="<%= CalculateLengthServerSide() %>"
You don't need to just use the data your binding to, you can call any server side function.
Edit: Try switching your label that stores the quantity to a textbox and making it read only so uses will not mess with it. After the button is clicked, you should be able to find the textbox control and read out the posted value.

Resources