When the user selects an "account" we dynamically create a linkbutton with the selected dates.
However, the linkbutton onclick handler doesn't fire when the button is clicked.
thx
Dim linkBtn As New LinkButton
linkBtn.Text = "blah"
AddHandler linkBtn.Click, AddressOf linkButtonHandler
linkBtn.ID = panelDatesRencontre.Controls.OfType(Of LinkButton).Count
panelDatesRencontre.Controls.Add(linkBtn)
ScriptManager1.RegisterAsyncPostBackControl(linkBtn)
Dim Trigger1 As New AsyncPostBackTrigger
Trigger1.ControlID = linkBtn.ID
Trigger1.EventName = "Click"
UpdatePanel2.Triggers.Add(Trigger1)
Answer: i make that in page_load, it seems to work
In order for the handler to be available it has to be recreated in the page_init section.
Otherwise you'll just get a postback without the handler executing.
Related
The datagrid reads from several xml files, so I create the columns dynamically, and added a templatefield as the last column.
A link button is added in the templatefield using RowDataBound.
Private Sub GridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridItem.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
Dim linkb As New LinkButton
linkb.Text = "Delete"
linkb.ID = "LinkDeleteItem"
linkb.OnClientClick = "javascript:DeleteItem('" & Convert.ToString(e.Row.RowIndex) & "')"
e.Row.Cells(GridItem.Columns.Count - 1).Controls.Add(linkb)
End If
Catch ex As Exception
lblMessage.Text = ex.Message
End Try
End Sub
Everything works fine.
But when I click a button outside the gridview, to open a window to add a new item to the grid, the linkbuttons disappear. But the column is still there.
If I just close the new window without saving a new data (which will prompt the grid to rebind), the column remains empty. I had to reload the gridview for the linkbuttons to appear.
Is it because the linkbuttons are created on rowdatabound? How should I solve this?
This is happening because the OnRowDataBound event of GridView is NOT called on next postback. This happens because by default viewstate of GridView is set true i.e. EnableViewState property of Gridview is true. When ViewState is on, Gridview stores data from the same and OnRowDataBound event is not called. Also, at this point View state will not be applied for your linkButtons as they aren't created in page_load.
Try setting EnableViewState property to false for your gridview:
<asp:GridView ID="CustomersGridView"
OnRowDataBound="CustomersGridView_RowDataBound"
EnableViewState="false"
.... />
OR you can also bind your GridView in page_Load as:
protected void Page_Load(object sender, EventArgs e)
{
CustomersGrIdView.DataBind();
}
Now after every postback your OnRowDataBound event will be called and hence the LinkButtons will be available everytime.
NOTE: Setting EnableViewState property to false for gridview can be a bad practice to an extent, especially in scenarios of heavy data usages. Same goes for Binding gridview every time.
When using dynamic controls, they exist only until the next postback.ASP.NET will not re-create a dynamically added control. If you need to re-create a control next time too on postback, you should perform the control creation in the PageLoad event handler.
This will give you benefit of using view state with your dynamic control. Even though view state is normally restored before the Page.Load event, if you create a control in the handler for the PageLoad event, ASP.NET will apply any view state information that it has after the PageLoad event handler ends.
i have a custom control which made by CodeBehind only (Class Library)
the control will create a asp button with a click event
i create the button in RenderControl method
and then set the event handler
Button m_oBTN = new Button();
m_oBTN.Text = "Submit";
m_oBTN.ID = "btnSubmit";
m_oBTN.CssClass = "btnSubmit";
m_oBTN.Click += new System.EventHandler(this.btnSubmit_Click);
however, it can success render the button, but no event has trigger when button click
do i need to use method like rasieEvent.....(i forgot name)?
how can i trigger the event when button is click?
Most probably because you are not setting the event handler correctly! This line should be:
m_oBTN.Click += new System.EventHandler(m_oBTN);
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
i wrote following code to create a linkbutton programmatically, but its showing like lable at runtime not as link
Dim lnkbutton As LinkButton = New LinkButton()
lnkbutton.ID = "HostelsClub" & dr("dshotelid").ToString()
lnkbutton.Text = "HostelsClub"
lnkbutton.PostBackUrl = Request.Url.ToString()
lnkbutton.ToolTip = "Click here To Book"
AddHandler lnkbutton.Click, AddressOf lnkBook_Click
PHbook.Controls.Add(lnkbutton)
problem is not with your link button you need to add event handler for link button in page's init function .. than only it will work..