So I have this line of code which uses a hyperlink
<asp:HyperLink style="text-decoration:none" runat="server" Text="View Order" NavigateUrl='<%# "OrderDetails2.aspx?oID=" + Eval("oID") %>'>/asp:HyperLink>
Works perfectly fine but when I try to use a button to do the same thing (since I have to use an OnClick method), it doesn't allow me to use NavigateUrl and Eval in it. Any alternative methods?
Thats because asp:button does not have a property called NavigateUrl, its only a valid property for an hyperlink.
You can use JavaScript click event for an asp:hyperlink.
Not sure what you're trying to do.
If you are trying to add a link to another page, just use asp:hyperlink but use CSS to make it look like a button.
If you really do want a button with a server-side click event, you can add the following to the end of the click event handler:
Response.Redirect(String.Format("OrderDetails2.aspx?oID={0}", oID));
Related
In vb .aspx Inside data-content attribute i have my TextBox and Button Control . But there id not visible in code-behind .i need to fire evnt on button. how to fire event on button??
<a
data-content='<asp:TextBox runat="server"></asp:TextBox><asp:Button ID="btnEdit" runat="server" Text="Edit" />'
data-html='true' data-toggle='popover' href='#' title='UserName'><%#Eval("Name")%></a>
I need to fire event in code-behind... Any Suggestion
If memory serves me correctly, the ID of an asp control, like asp:Button is essentially the name of the control variable in code-behind. You need to reach it from something like the aspx.vb file
The attribute of OnClick="Event_Name" could be helpful in this example.
If you need a client-side click, then you might want to work with the ClientID data member or the onclick html attribute of the generated tag.
I am trying to right click on a LinkButton and open it in a new tab or seperate window page displays nothing. I found some solution for the use of Hyperlink button as it has property to set target to "_blank" but LinkButton has not any target attribue.
I want to use LinkButton instead of hyperlink button, its just because i can't set command arguement or command name on hyperlink button and can't fire an event on it.
<asp:LinkButton ID="lnkHeadingHindi" Text='<%#Patrika.Common.ConvertNews(Eval("strMainHeadingHin").ToString())%>' CommandArgument='<%#Eval("intNewsId") %>' runat="server"></asp:LinkButton>
It would be great if anybody has a solution and let me know in case of any concern.
Thanks !!
Based on the accepted answer to this question, if you want to perform a POST (such as a LinkButton does) but have the result open in a new window, you need to add target="_blank" to the form on the page, not the link itself.
Obviously you don't want to do this when you initially render the page, as everything that caused a postback would open in a new window.
Instead, try adding the following attribute to your LinkButton:
OnClientClick="$('form').attr('target', 'blank')"
This will dynamically set the form attribute just before the form is posted back by a click to the link.
Note that this doesn't give the right-click functionality you want, but it does work to open in a new window on a left-click.
If you don't have access to JQuery, you'll need to do something like
protected void Page_PreRender(object se, EventArgs e)
{
this.Page.Form.ID = "someUniqueID"; // unless your form already has an ID
yourLinkButton.OnClientClick =
"document.getElementById('" +
this.Page.Form.ClientID +
"').setAttribute('target', '_blank')";
}
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:
1) Use a HyperLink control, and set the Target property
2) Provide a method to the OnClientClick property
that opens a new window to the correct place.
3) In your code that handles the PostBack add some JavaScript to fire on PageLoad that will open a new window correct place.
If you like when click the link button,then open new window,
please see this
insert base tag like this
<head>
<base target="_blank" />
</head>
If you want to pass some information to the page using hyperlink, pass it in the url using QueryString.
<asp:HyperLink id="hyperlink1"
NavigateUrl="~/MyPage.aspx?intNewsId=10"
Text="ClickMe"
Target="_blank"
runat="server"/>
You can't have it both ways, you'll have to choose between:
1) the linkbutton executing some code in the current page
2) a hyperlink opening another page in a new window
I guess you want to have the linkbutton executing some code in the new page, but that's impossible.
or if u like use . anger tag
Just render an anchor with href set to appropriate url and set the target attribute to _blank it will open the url into new window
<a href="urlOfThePage" target="_blank" >Click me</a>
Hi I'm trying to learn more JavaScript AJAX. Basically I would like to have a popup Yes No on a delete button that would precede the actual C# event being fired. I could do this all in C# but I know doing it client side would be beneficial as it would reduce server load.
I'm not sure exactly how to go about this. Any ideas?
You can use the javascript function called confirm
onclick="return confirm ('Are you sure you want to delete this _____?');"
This text parameter is the text that will be shown in the modal with the yes and no.
The Yes returns true which allows the postback to continue, but the No returns false and will stop the postback.
EDIT:
As mentioned by XaiSoft and Cybernate, if you are using an ASP.NET button you can also use the OnClientClick property which will be translated to onclick by the server.
You can use OnClientClick property of the asp:Button.. along with the JS confirm..
Try something like the code below in your aspx/ascx/master:
<asp:Button id="cmdSubmit" OnClientClick="return confirm('Are you sure?')" runat="server" .../>
You can also use jQuery instead of the ugly default confirm. Here is a question addressing that.
I want to develop an application in asp.net with C#.
In that application there are one text box and one label.
I want the following functionality:
When I press any key in textbox then I should get this value on .cs page i.e; code behind. And from .cs page I want to add this value to the label.
The problem is that there is no keypress event for an asp textbox and if I take a html text box then I don't get its value on .cs page
How can I come out with this problem?
Because a keypress on a textbox is a client side event but you want to perform server-side processing you will need to use AJAX requests.
You may find the following useful:
AJAX Toolit
Using Jquery to call asp.net page methods
In asp.net the TextBox will have TextChanged event but you will need to enable post back for the button and the event will fire when you tab out of the TextBox.
For the task you want either use javascript or add a button and when this button is do what you want.
I don't think this is a good aproach in web app., In this way you will end with a lot of post-backs.
However, if you still want this functionality. Texbox has TextChanges event, and if you also change the textboxs's AutoPostBack property to true you will get something close, but you will still have to move a currsor.
But it is still a terible solution. Why don't you simply use a button that fires click event instead?
Alternative solution is to use Ajax or javaScript,..
You can simply create a JavaScript-Method for this.
Your Textbox:
<asp:TextBox ID="textBox" runat="server" onkeydown="onFilterTextChanged()">
</asp:TextBox>
Your JavaScript, do a TimeOut to not do this every 0,0001 secs.
function onFilterTextChanged() {
if (timeoutID)
window.clearTimeout(timeoutID);
timeoutID = window.setTimeout(updateFilterText, 600);
}
Send the Values to the CodeBehind, textis your TextBox-Text.
function updateFilterText() {
var text = document.getElementById("<%=textBox.ClientID %>").value;
__doPostBack("<%=textBox.ClientID%>", "CommandArg" + text);
}
You won't need to do as many PostBacks as with the native TextChanged-Event and you can simply use this e.g. for Auto-Extender-Plugins. Pack the TextBox into an UpdatePanel and you're good to go!
Unless of course you do not NEED to go back to the server, in which case just set the labeltext in updateFilterText.
Ugh, this is driving me mad
Im trying to build up a dynamic menu from a bulletedList, most menu items are plain links however the log out button needs to perform some cleanup code.
I cant for the life of me get the BullettedLists onclick event to fire.
The BulletedList is inside a user control (if that makes a difference)
Any ideas?
Or - any ideas for an alternative, better solution?
Code below
BulletedList
<asp:BulletedList OnClick="menu_Click" runat="server" CssClass="MainMenu" ID="loggedInMenu" DisplayMode="HyperLink" />
Adding an element
loggedInMenu.Items.Add(new ListItem("Logout", ""));
Click handler
protected void menu_Click(object sender, BulletedListEventArgs e)
{
user.logout();
Response.Redirect("Default.aspx");
}
You're using the wrong DisplayMode for your BulletedList control. You should use a DisplayMode of LinkButton. When you use DisplayMode.HyperLink:
Users can click links to move to
another page. You must provide a
target URL as the Value property of
individual items.
This is from the MSDN docs for this control. (It's about 3/4 of the way down the page.)
When you use a BulletedList control in HyperLink mode, the value of your ListItem is the URL that you're navigating to. So your static page HTML controls would use ListItem.Value as the href attribute of the <a> tag.
Here's what the HTML markup looks like when you use a DisplayMode of HyperLink (it's a plain old HTML anchor tag w/ a href):
<li>One</li>
But since you want to postback, you should set the DisplayMode of your BulletedList control to LinkButton. When you do that, you'll enable a postback back to your page and your event handler will trap the event. You can then process the click appropriately then. The event argument that's passed in (of type BulletedListEventArgs) will have an Index property, and that will tell you what item in your list was clicked.
Here's the updated .aspx code that I used:
<asp:BulletedList ID="bullet" runat="server" DisplayMode="LinkButton"
onclick="bullet_Click">
<asp:ListItem Text="One" Value="1">One</asp:ListItem>
</asp:BulletedList>
Everything else is the same except the DisplayMode, which is set to LinkButton. When I use that, then my bullet_Click event handler is fired when I click a list item.
I hope this helps!!