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
Related
On my aspx page I have a gridview template that when it is rendered has 100 - 200 rows and each row has a check box.
When the page loads 5 check boxes are automatically checked. If I manually check another 3 then press the submit button, then loop through all the gridview rows to find out which rows had a checked box then it still remembers the original 5. How do I make it remember the updated 8?
Protected Sub mySubmitButton_Click(sender As Object, e As EventArgs)
Dim myArray As ArrayList = New ArrayList()
For Each myRow As GridViewRow In MyGridview.Rows
If CType(myRow.FindControl("MyCheckbox"), CheckBox).Checked Then
myArray.Add(MyGridview.DataKeys(MyRow.RowIndex).Value)
End If
Next
'add to DB myArray
End Sub
I tried this on the gridview in the asp.net and it made no difference
ViewStateMode="Disabled" and ViewStateMode="Enabled"
Thanks for the help
Is the ViewState enabled on the GridView?
Have you got the logic to load your Grid in the Page_Load event, if so add it in if(!IsPostBack)
I'm using the FindControl function to look for a control on the page. It seems super simple and straight forward on MSDN but I can't get it to find the control. The page I'm using has a MasterPageFile that prepends more to the id that I give the contorl in the aspx file. A simple example that isn't working:
aspx page
<%# Page Title="Inventory Control Test" Language="VB" AutoEventWireup="false" MasterPageFile="~/Site.master" CodeFile="Default2.aspx.vb" Inherits="Sales_ajaxTest_Default2" %>
<asp:Content ID="conHead" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="conBody" ContentPlaceHolderID="MainBody" Runat="Server">
<asp:Button ID="saveAllBtn" runat="server" Text="Save All" />
</asp:Content>
code behind
Partial Class Sales_ajaxTest_Default2
Inherits System.Web.UI.Page
Protected Sub saveAllBtn_Click(sender As Object, e As System.EventArgs) Handles saveAllBtn.Click
Dim myControl1 As Control = FindControl("ctl00_MainBody_saveAllBtn")
If (Not myControl1 Is Nothing) Then
MsgBox("Control ID is : " & myControl1.ID)
Else
'Response.Write("Control not found.....")
MsgBox("Control not found.....")
End If
End Sub
End Class
I get that msgbox isn't a web thing I'm just using it for this example.
If i use "saveAllBtn", which is the id given to the control, in the FindControl I get "control not found". If I try this, on a stand alone page without a masterpage it works fine.
If I inspect the element using chrome I find that the ID of the button has been changed to "ctl00_MainBody_saveAllBtn" but if I use that in the FindControl I still get "control not found"
When you use FindControl you would specify the "server ID" (what you named it) of the control, not the final rendered "client ID" of the control. ex:
Dim myControl as Control = MainBody.FindControl("saveAllBtn")
However, in your specific example, since you are in the saveAllBtn.Click event, the control you are looking for is actually the sender parameter (because you clicked on that button to trigger the event you are in) ex:
Dim myControl as Button = CType(sender, Button)
If you just want to find saveAllBtn control, wweicker's second method using CType(sender, Button) is the prefer one.
However, if you want to find other control by name, you cannot use just FindControl. You need to find the control recursively, because it is nested inside other controls.
Here is the helper method -
Protected Sub saveAllBtn_Click(sender As Object, e As EventArgs)
Dim button = TryCast(FindControlRecursive(Me.Page, "saveAllBtn"), Button)
End Sub
Public Shared Function FindControlRecursive(root As Control, id As String) As Control
If root.ID = id Then
Return root
End If
Return root.Controls.Cast(Of Control)().[Select](Function(c) FindControlRecursive(c, id)).FirstOrDefault(Function(c) c IsNot Nothing)
End Function
Note: My VB code might be a bite weird, because I wrote in C# and converted to VB using converter.
FindControl does not work recursively. You must start at one point (Me, for example), and if that is not the control your looking for, search the Controls collection of your starting point. And so forth.
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.
I'm trying to add a set of LinkButtons to the PagerRow of a GridView in an UpdatePanel. I'm using the RowCreated event to instantiate and add these. The problem I have is the click handlers are only fired the second time.
I create the LinkButtons using
Protected Sub grd_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.Pager) Then
Dim pageSizer = New GridViewPageSizer(grdItems)
e.Row.Cells(0).Controls.AddAt(0, pageSizer)
End If
End Sub
To create the LinkButtons themselves, I'm using
Dim lnkSize = New LinkButton() With { _,
.Text = size.ToString(), _
.CommandArgument = size.ToString(), _
.ID = "pageSizer" & size
}
AddHandler lnkSize.Click, AddressOf lnkPageSize_Click ' an EventHandler which just changes pagesize based on CommandArgument
liSize.Controls.Add(lnkSize)
The GridViewPageSizer inherits HtmlGenericControl and adds an event handler for the click of each button. On every postback, the pager row is recreated, so the old buttons are replaced with a new set and their event handlers only fire on the second click. If I check for !IsPostBack, the buttons disappear after the first click. I've tried rebinding the grid after the buttons are clicked and the pagesize changes but the same thing happens. Is there a way around this?
In order for an EventHandler to trigger correctly, you need to bind it on Page Init/PreInit.
Make a session indicator and on postback, check that session on Page Init.
If that satisfies the condition, instead of recreating it on RowCreated Event, recreate the controls together with the Events on Page Init instead.
This time, events will trigger correctly.
I have a navigation bar which is dynamically populated with LinkButtons by an ASP repeaterControl.
I have no problem accessing, and setting properties for the clicked LinkButton. This I can do using the sender object from the fired LinkButton. Once a LinkButton is clicked, it is highlighted in bold.
My problem is to clear the bold property of the previously clicked linkButton when a new LinkButton ( another RepeaterItem within the same repeater) is clicked.
Any ideas on this, please? Many thanks!
ps.
I cannot access the buttons through their ID, since they all have the same ID within the repeater.
I have unique arguments on each RepeaterItem (CommandArgument), but when I try to iterate through all linkbuttons, only static linkbuttons are found, none inside the repeater. See below:
Dim c As Control
For Each c In Form1.Controls
If TypeOf c Is LinkButton Then
MsgBox(DirectCast(c, LinkButton).CommandArgument)
End If
Next c
Try this:
For each item as RepeaterItem in YourRepeaterControl.Items
Dim button as LinkButton = item.FindControl("YourLinkButtonId")
If button IsNot Nothing Then
'Do whatever you want here
End If
Next