issue with ASP.Net Linkbutton and CSS class (visited) - asp.net

I have an asp.net (.Net Framework 4.0) page with a gridview in it which contains linkbuttons. I set the cssclass property of the linkbuttons to make sure, the Color of the text changes when the link is being clicked (visited). Unfortunately that doesn't work... when i click on the linkbutton, the Color changes, then the postback occures and when i go back to that page, the linkbutton has the original Color :-( (tested with IE10, in IE8 and Firefox the Color even changes to the standard-link Color - not the Color i set, it also changes back immediately).
I tried it with Asp:Hyperlink which works, but i have many pages in my web with linkbuttons and wanted to avoid changing every page...
This is my code:
<asp:LinkButton ID="lnkDetails" runat="server" Text='<%# eval("str_Betreff") %>'
CssClass="westsitelinkbutton" OnClick="lnkDetails_Click"></asp:LinkButton>
This is the css code:
.westsitelinkbutton a:visited
{
color: #FFFFFF;
}
And here is the code within "lnkDetails_Click":
Public Sub lnkDetails_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim btn As LinkButton = CType(sender, LinkButton)
Dim row As GridViewRow = DirectCast(btn.NamingContainer, GridViewRow)
Response.Redirect("mitarbeiter_gesendet_details.aspx?id=" & gvGesendeteObjekte.DataKeys(row.RowIndex).Value.ToString())
Catch ex As Exception
objFunctions.ShowAlert("fehler", ex.Message, ClientScript, Page)
End Try
End Sub
Can anyone help me with this issue please?
Kind regards,
Sabrina

Related

Double clicking fire of dynamically generated web controls

Dynamic button controls in a panel existing. Clicking on specific button needs to be disabled while the event is firing to prevent double click submission. Can any one suggest me some ideas? Thanks in advance.
To Use in HTML:
<asp:LinkButton Style="color: red; font-family: verdana" runat="server" ID="LinkEliminar">Eliminar</asp:LinkButton>
To Use in ASPX
Protected Sub LinkEliminar_Click(sender As Object, e As EventArgs) Handles LinkEliminar.Click
Label16.Text = "¿Eliminar Requisición No." & Label14.Text & "?.-"
eliminar.Visible = True
End Sub

Rise event from user control written in main page

I have searched this site for what I need, but none of the answers just quite fits my needs. So here's the deal:
I am dynamically loading a usercontrol to aspx page along with some buttons like this:
Dim uc As UserControl
Dim btns As New List(Of LinkButton)
uc = LoadControl("path_to_ascx")
btns.Add(New LinkButton With {.ID = "btnid", .Text = "Sometext"})
uc.GetType().GetProperty("tblButtons").SetValue(uc, btns, Nothing)
holder2.Controls.Add(uc) 'holder2 is an id of a PlaceHolder
An this work perfectly fine. The buttons show on the page as expected. Now I am having trouble, how to tell these buttons to rise an event written in aspx page, to which a usercontrol is being loaded to.
Public Sub btnClick(sender As Object, e As EventArgs)
'do stuff
End Sub
Why I want to achieve this? Because I have a pretty complex UserControl which I want to reuse as much as possible, but buttons will do different stuff on every aspx page this UserControl will be loaded to.
Thanks
I have solved my problem. In UserControl, I added dynamic buttons to every row on OnRowCreated event.
I was loading UserControl to aspx with buttons like in my question, I just added an ID property to my usercontrol:
Dim uc As UserControl
Dim btns As New List(Of LinkButton)
uc = LoadControl("path_to_ascx")
btns.Add(New LinkButton With {.ID = "btnid", .Text = "Sometext", .CommandName = "cmdName"})
uc.ID = "UserControl1" 'here I added ID property
uc.GetType().GetProperty("tblButtons").SetValue(uc, btns, Nothing)
holder2.Controls.Add(uc) 'holder2 is an id of a PlaceHolder
And after I add an EventHanlder like this:
AddHandler TryCast(holder2.FindControl("UserControl1").FindControl("grid"), GridView).RowCommand, AddressOf grid_RowCommand
'grid is the ID of GrdiView in UserControl
And here is the event for gridview rowCommand written in aspx code behind:
Protected Sub grid_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "someCmdName" Then
'do stuff
Else
'do somthing else
End If
End Sub
Maybe my question was not good enough, because I did not mention, that I will be loading buttons to gridview row on rowCreated event and hook them up to RowCommand for wich I apologise.
If someone knows another way to do this, it would be much appreciated to share.
Regards

Trigger Anchor Tag with LinkButton onclick function vb.net

as you know, Fancybox comes with the anchor tag
I am currently working on a project of a datalist using vb.net
When I click the Linkbutton, I am suppose to get the command argument and set it to session and then open up the fancybox. As fancybox has its own restriction to anchor tags, it is only workable on the first item of the datalist
So I want to do it in such a way that when I click the linkbutton, I will pass the session and also trigger the anchor tag from the code behind. Here is the codebehind for the button_click for the linkbutton
Protected Sub Button1_Click(sender As Object, e As System.EventArgs)
'Dim addJobRequest As Button = sender
' Dim jobID As String = addJobRequest.CommandArgument
' addJobtoRequest(jobID)
Dim button As LinkButton = sender
Dim itegerr As Integer = button.CommandArgument
Session("jobID") = itegerr
Return
'From here, I need a code that can trigger anchor tag
End Sub
Here is the code for the anchor tag
<a id="fancybox-manual-b" href="javascript:;">
P.s. I have tried the linkbutton href function and but it doesn't work.

Losing <asp:Label> Text value from ViewState for dynamically added control

I am adding controls to a page programatically in the code behind. I add an asp:Label and set it's Text value. I add an asp:TextBox and set it's Text value. Both Text values are returned in the Response and displayed in the browser. All fine so far.
The user performs an action that causes a postback. I re-load the dynamically added asp:Label and asp:TextBox. When the Response is returned to the browser, only the asp:TextBox Text value is displayed. The asp:Label Text value is not.
If I inspect the HTML I can see the asp:Label control (rendered as an HTML span tag) but no value.
How can I get the code to automatically re-load the Text value of an asp:Label on each postback? Why is the behaviour different for an asp:Label and an asp:TextBox? I do not want to have to manually re-set the Text value on each postback.
Here is some code similar to what I am doing (placeHolderNameplates is an asp:PlaceHolder control on the aspx page):
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not Page.IsPostBack Then
Dim lbl As Label = New Label()
lbl.ID = "xxx1"
lbl.Text = "yo"
placeHolderNameplates.Controls.Add(lbl)
Dim tb As TextBox = New TextBox
tb.ID = "xxx2"
tb.Text = "yoyo"
placeHolderNameplates.Controls.Add(tb)
Else
Dim lbl As Label = New Label()
lbl.ID = "xxx1"
placeHolderNameplates.Controls.Add(lbl)
Dim tb As TextBox = New TextBox
tb.ID = "xxx2"
placeHolderNameplates.Controls.Add(tb)
End If
What you need to do is add the control to the placeholder before setting the values, so it should be
Dim lbl As Label = New Label()
placeHolderNameplates.Controls.Add(lbl)
lbl.ID = "xxx1"
lbl.Text = "yo"
See these posts for details:
http://www.yakkowarner.com/2008/01/aspnet-dynamic-controls-and-viewstate.html
http://codebetter.com/jefferypalermo/2004/11/25/key-to-ensuring-dynamic-asp-net-controls-save-viewstate-level-300/
Before they are added to the page, they have not initialized themselves. When a dynamic control is added to another control, the new control plays catch-up to get to the stage that the parent control is in. For instance, if in your Page_Load, you add a textbox, it will play catch-up and go through its Init and Load phases. This is important beceause it will start tracking its viewstate. Values added before it is tracking viewstate won’t make it to viewstate and will be lost on PostBack.
It seems like dynamically created controls won't be added to the ViewState automatically. The TextBox Control retains it's value however because of it's nature of being rendered to a <input type="text" value="xyz" /> html element.
Have a look at this article:
http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i
Hey check thsi site MSDN
You have to add your control with following event( so viewstate maintain automaticly)
override protected void OnInit(EventArgs e)
example of Add Dynamic control
http://support.microsoft.com/kb/317794/en-us

Including eval / bind values in OnClientClick code

I have a need to open a popup detail window from a gridview (VS 2005 / 2008). What I am trying to do is in the markup for my TemplateColumn have an asp:Button control, sort of like this:
<asp:Button ID="btnShowDetails" runat="server" CausesValidation="false"
CommandName="Details" Text="Order Details"
onClientClick="window.open('PubsOrderDetails.aspx?OrderId=<%# Eval("order_id") %>',
'','scrollbars=yes,resizable=yes, width=350, height=550');"
Of course, what isn't working is the appending of the <%# Eval...%> section to set the query string variable.
Any suggestions? Or is there a far better way of achieving the same result?
I believe the way to do it is
onClientClick=<%# string.Format("window.open('PubsOrderDetails.aspx?OrderId={0}',scrollbars=yes,resizable=yes, width=350, height=550);", Eval("order_id")) %>
I like #AviewAnew's suggestion, though you can also just write that from the code-behind by wiring up and event to the grid views ItemDataBound event. You'd then use the FindControl method on the event args you get to grab a reference to your button, and set the onclick attribute to your window.open statement.
Do this in the code-behind. Just use an event handler for gridview_RowDataBound. (My example uses a gridview with the id of "gvBoxes".
Private Sub gvBoxes_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBoxes.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim btn As Button = e.Row.FindControl("btnShowDetails")
btn.OnClientClick = "window.open('PubsOrderDetails.aspx?OrderId=" & DataItem.Eval("OrderId") & "','','scrollbars=yes,resizable=yes, width=350, height=550');"
End Select
End Sub

Resources