Render hyperlink button through gridview - asp.net

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!

Related

Adding a click event to a Hyperlink displayed in ListView VB.NET ASP.NET

Is there a way to add a click event to a Hyperlink tag that's render in ListView?
Basically, I have a HyperLink tag that generates a link dynamically and it opens up to a new tab when users click on it. At the same time, when the user click on it, I want to post a text or make the text label visible. Sample code below:
<asp:ListView ...>
<ItemTemplate>
<asp:Label ID="Msg" Text="*You have already accessed this link*" runat="server" Visible="false"/>
<asp:HyperLink ID="label1" NavigatUrl='<%#Eval("Link")%>'Target="_blink" text="Click Link" runat="server"></asp:HyperLink>
<//ItemTemplate>
</asp:ListView>
You can use QueryString. Add the link with a Query Parameter on it and then -
If IsPostBack=True then 'Check if the page is reloading [Because when you clik on the link with the same link, it will reload the page]
'Considering the QueryString will be like - "yourdomain/default.aspx?item=1"
'Check for QueryString
Dim s_ItemId As String = Request.QueryString("item")
if s_ItemId<>"" then
'Do whatever you want
End If

Disabling an ASP.net textbox without actually disabling it?

Within an ASP.Net application I have, there is a textbox that gets a date from a CalendarExtender. When the textbox is populated it checks that date with another date on the form and displays a modalpopupextender popup if the dates are wrong. However, I DO NOT want to allow user input into this textbox, so when I set the ReadOnly field to false and tried Enabled to false, it doesn't allow manual entry however it ALSO disabled the postback and will not call the TextChanged event to fire the modalpopupextender.
So is there a way to disable manual entry and not set it to ReadOnly?
I figured it out, simply enter onkeypress="return false;" within the HTML tag
Try this
<asp:textbox id="txt1" onfocus="blur()" runat="server"/>
this worked for me.
Add the below properties in the tag of textbox
onkeydown="return false" onpaste="return false"
ex:
<asp:TextBox ID="TillDate_TextBox" runat="server" onkeydown="return false" onpaste="return false"></asp:TextBox>
the first property block typing in textbox and the second property block pasting in it
I'm not familiar with the exact components you are using, however the usual way to accomplish things like this is the following. Have selecting the date on the calendar modify the value of a hidden form field. This will restrict the user from editing the value directly. Then create another element like a div or a span, and use javascript to update the span/div to the value selected on the calendar.

link button property to open in new tab?

In my application I have some link buttons there but when I right click on them I cannot (they are in disable mode) find the menu items Open in new tab or Open in new window.
How do I show those menu items?
Code example:
<asp:LinkButton id="lbnkVidTtile1" runat="Server" CssClass="bodytext" Text='<%#Eval("newvideotitle") %>' />
From the docs:
Use the LinkButton control to create a hyperlink-style button on the Web page. The LinkButton control has the same appearance as a HyperLink control, but has the same functionality as a Button control. If you want to link to another Web page when the control is clicked, consider using the HyperLink control.
As this isn't actually performing a link in the standard sense, there's no Target property on the control (the HyperLink control does have a Target) - it's attempting to perform a PostBack to the server from a text link.
Depending on what you are trying to do you could either:
Use a HyperLink control, and set the Target property
Provide a method to the OnClientClick property that opens a new window to the correct place.
In your code that handles the PostBack add some JavaScript to fire on PageLoad that will open a new window correct place.
Here is your Tag.
<asp:LinkButton ID="LinkButton1" runat="server">Open Test Page</asp:LinkButton>
Here is your code on the code behind.
LinkButton1.Attributes.Add("href","../Test.aspx")
LinkButton1.Attributes.Add("target","_blank")
Hope this will be helpful for someone.
Edit
To do the same with a link button inside a template field, use the following code.
Use GridView_RowDataBound event to find Link button.
Dim LB as LinkButton = e.Row.FindControl("LinkButton1")
LB.Attributes.Add("href","../Test.aspx")
LB.Attributes.Add("target","_blank")
try by Adding following onClientClick event.
OnClientClick="aspnetForm.target ='_blank';"
so on click it will call Javascript function an will open respective link in News tab.
<asp:LinkButton id="lbnkVidTtile1" OnClientClick="aspnetForm.target ='_blank';" runat="Server" CssClass="bodytext" Text='<%# Eval("newvideotitle") %>' />
This is not perfect, but it works.
<asp:LinkButton id="lbnkVidTtile1" runat="Server"
CssClass="bodytext" Text='<%# Eval("newvideotitle") %>'
OnClientClick="return PostToNewWindow();" />
<script type="text/javascript">
function PostToNewWindow()
{
originalTarget = document.forms[0].target;
document.forms[0].target='_blank';
window.setTimeout("document.forms[0].target=originalTarget;",300);
return true;
}
</script>
LinkButton executes HTTP POST operation, you cant change post target here.
Not all the browsers support posting form to a new target window.
In order to have it post, you have to change target of your "FORM".
You can use some javascript workaround to change your POST target, by changing form's target attribute, but browser will give a warning to user (IE Does), that this page is trying to post data on a new window, do you want to continue etc.
Try to find out ID of your form element in generated aspx, and you can change target like...
getElementByID('theForm').target = '_blank' or 'myNewWindow'
When the LinkButton Enabled property is false it just renders a standard hyperlink. When you right click any disabled hyperlink you don't get the option to open in anything.
try
lbnkVidTtile1.Enabled = true;
I'm sorry if I misunderstood. Could I just make sure that you understand the purpose of a LinkButton? It is to give the appearance of a HyperLink but the behaviour of a Button. This means that it will have an anchor tag, but there is JavaScript wired up that performs a PostBack to the page. If you want to link to another page then it is recommended here
that you use a standard HyperLink control.
It throws error.
Microsoft JScript runtime error: 'aspnetForm' is undefined
<asp:LinkButton ID="LinkButton1" runat="server" target="_blank">LinkButton</asp:LinkButton>
Use target="_blank" because It creates anchor markup. the following HTML is generated for above code
<a id="ctl00_ContentPlaceHolder1_LinkButton1" target="_blank" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LinkButton1','')">LinkButton</a>

How to get value in a textbox that is readonly

I have a textbox.In its onclick iam calling a javascript fn to call calendar.Texbox is readonly. Clicking on textbox calendar is coming and value is showing in textbox. But on clicking submit button where I have written code to save, there i am not getting value in textbox. What may be the reason for that?
I suspect that your text box has the disabled attribute instead of readonly which prevents it from posting its value to the server.
I had this problem too, there is a really simple workaround
Instead of making the textbox ReadOnly using properties option. Do it thru the code behind adding this code:
YourTextBox.Attributes.Add("readonly", "readonly");
You can get the value by using
Request.Form[YourTextBox.UniqueID]
I'm guessing the textbox is disabled so that people have to use the calendar control to enter a date? The textbox is just for showing the selected date?
If you want the textbox to stay readonly (i.e. disabled client-side), have a hidden input that has the value you want to handle on the server is the way to go probably. So add an additional input for to the page. Use
<asp:HiddenField ... runat="server" />
So the client-side code that updates your readonly textbox will also update your hidden input.
if readonly property is true, its not break your .cs call. you can use this as always:
here are your inputs:
<asp:TextBox ID="txtBraid" runat="server" Text="Im sooo readonly" ReadOnly="True"></asp:TextBox>
<asp:Label ID="lblBraid" runat="server" Text="Im gonna change, i promise"></asp:Label>
on .cs page put these to onClick function or something:
lblBraid.Text = txtBraid.Text;

ASP:LinkButton and Eval

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.

Resources