I have a dynamic table being build on page_load.
I put the table inside an update panel so that I could add rows dynamically without a full page postback. Each row has 4 cells first 3 containing textboxes and the 4th cell being an image button that needs to delete the row.
However the image button to remove the row is posting back but not firing the event handler sub.
I set the table to a session variable for reloading on postbacks.
<%--Update Panel with Image Button to add row--%>
<asp:UpdatePanel ID="pnlHA" runat="server" UpdateMode="conditional" >
<ContentTemplate>
<asp:ImageButton ID="imgAddHA" AlternateText="Add Row" ImageUrl="../images/plus_orange.png" style="width:17px; height:17px; cursor: hand;" runat="server" />
<div style="height:100px; overflow:scroll; overflow-x:hidden;">
<span runat="server" id="spanTBL_HA" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
on page.init if the pnl page is in postback then it recreates the span
If pnlHA.Page.IsPostBack Then
spanTBL_HA.Controls.Clear()
spanTBL_HA.Controls.Add(CType(Session("tblHA"), Table))
End If
Image button being added to table cell
img = New ImageButton
Session("tblHA_Counter") = CInt(Session("tblHA_Counter")) + 1
img.AlternateText = "Delete Row"
img.Attributes.Add("style", "height: 10px; width: 10px; cursor: hand;")
img.ImageUrl = "../images/minus_red.png"
img.ID = "img_" & CInt(Session("tblHA_Counter")).ToString
AddHandler img.Click, AddressOf imgRemove_Click
img.Attributes.Add("runat", "server")
tc.Attributes.Add("style", "width:35px")
tc.Controls.Add(img)
Click Event
Protected Sub imgRemove_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Dim img As ImageButton = CType(sender, ImageButton)
Dim delRow As TableRow = Nothing
Dim rowIndex As Integer = 0
For Each row As TableRow In CType(Session("tblHA"), Table).Rows
If CType(row.Cells(3).Controls(0), ImageButton).UniqueID = img.UniqueID Then
delRow = row
End If
Next
If delRow IsNot Nothing Then
CType(Session("tblHA"), Table).Rows.Remove(delRow)
End If
spanTBL_HA.Controls.Clear()
spanTBL_HA.Controls.Add(CType(Session("tblHA"), Table))
End Sub
Dynamic controls need to be recreated every time the page is created to allow ASP.NET to restore view state, postback data and event wireup to the dynamically created controls. So remember to recreate them on the postback as well.
Please check whether you are creating them on every post back.
Related
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
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
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've got a asp.net gridview and inside of the grid view I have a check box at the header of the grid view like so:
<HeaderTemplate>
<asp:CheckBox Width="1px" ID="HeaderLevelCheckBox" AutoPostBack="true" OnCheckedChanged="SelectAllRows" runat="server" />
</HeaderTemplate>
This gives me a nice little check box at the top of the grid view...the event OnCheckedChanged calls a function called SelectAllRows that looks like this:
Public Sub SelectAllRows(ByVal sender As Object, ByVal e As System.EventArgs)
Dim gr As GridViewRow = DirectCast(DirectCast(DirectCast(sender, CheckBox).Parent, DataControlFieldCell).Parent, GridViewRow)
Dim h As CheckBox = DirectCast(gr.FindControl("HeaderLevelCheckBox"), CheckBox)
For Each Row As GridViewRow In Me.gvLineItems.Rows
Dim cb As CheckBox = CType(Row.FindControl("chkSelector"), CheckBox)
cb.Checked = h.Checked
Next
End Sub
So if I click this header checkbox it checks all of the items in the gridview, and if I uncheck it, it unchecks all the items in the gridview. This works fine...but what doesnt seem to work is if the page loads up and I check the grid view header checkbox to true and it selects all the items in the gridview, then i click a button such as a DELETE button that calls some server side code. That code simply loops through the grid view and checks if the checkbox has been checked, if it is it calls code to delete an item. Something to this effect:
For Each Row As GridViewRow In Me.gvLineItems.Rows
Dim cb As CheckBox = CType(Row.FindControl("chkSelector"), CheckBox)
Dim lID As Long = Convert.ToInt32(gvLineItems.DataKeys(Row.RowIndex).Value)
If cb IsNot Nothing AndAlso cb.Checked Then
'ok to delete
End If
Next
When I place a watch and debug on this it seems that the value cb is always false...
Even though it was set to true when I clicked the header checkbox...
What gives ???
The actual chkSelector in the grid view is for each row and it looks like this:
<ItemTemplate>
<asp:CheckBox ID="chkSelector" runat="server" onclick="ChangeRowColor(this)" />
</ItemTemplate>
Also I am already checking for postback..that is not the issue, remember chkSelector does not autopostback...
Thanks
I doubt, your gridview is rebinding on a Delete button click, because a click of the Delete button loads the page first where it will rebind and your checkbox's become unchecked again. I think you are binding your gridview some where in the page load event.
You have to do something like this
If(!Page.IsPostBack)
{
//Gridview Binding Code goes here....
}
Edit: Alternatively you can check/uncheck rows using javascript. It will save a round trip to the server side and resolve your current issue as well.
Here is complete code
<script language="javascript" type="text/javascript">
function SelectAll(spanChk,grdClientID) {
var IsChecked = spanChk.checked;
var Chk = spanChk;
Parent = document.getElementById(grdClientID);
var items = Parent.getElementsByTagName('input');
for(i=0;i<items.length;i++)
{
if(items[i].type=="checkbox")
{
items[i].checked=document.getElementById(spanChk).checked;
}
}
}
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkHeader" onclick="SelectAll('<%=chkHeader.ClientID %>, <%=yourGrid.ClientID %>') />
</HeaderTemplate>
I have a simple dropdownlist(ffg)...
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" BackColor="LightSteelBlue" Font-Size="X-Small"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged1" Style="z-index: 102; left: 37px; position: absolute; top: 85px" Width="331px"
</asp:DropDownList>
which I bind data to usind the onpageload event...
DropDownList2.DataSource = td.DataSet
DropDownList2.DataSource = td
DropDownList2.DataTextField = td.Columns("Name").ColumnName.ToString
DropDownList2.DataValueField = td.Columns("VendorCode").ColumnName.ToString
DropDownList2.DataBind()
and an onleselectedindexchaged event where I try to retreive the new value like this...
Protected Sub DropDownList2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.TextChanged
Dim url As String = "sp_menu.aspx?sp=" & DropDownList2.SelectedValue
Session.Remove("sp")
Session("sp") = DropDownList2.SelectedValue
Session("spnm") = DropDownList2.SelectedItem.Text & " (" & DropDownList2.Text & ")"
Response.Redirect(url)
End Sub
But it always brings the first value no matter which one is clicked on the dropdownlist.
Please help!
Ok... a few things...
First
DropDownList2_TextChanged isn't wired to your DropDownList so I can't see how that event would ever fire unless you're doing the wireup in your codebehind
Second
You say this code here
DropDownList2.DataSource = td.DataSet
DropDownList2.DataSource = td
DropDownList2.DataTextField = td.Columns("Name").ColumnName.ToString
DropDownList2.DataValueField = td.Columns("VendorCode").ColumnName.ToString
DropDownList2.DataBind()
is in your PageLoad event. Have you wrapped it in an If Not IsPostBack,
because if not, then you'll rebind every time, and lose your previous selection.
When you're databinding in Page_Load, you're essentially also resetting the selecteditem.
You should wrap whatever bindingcode that exists in Page_Load inside an
if(!IsPostBack) block.
EDIT: ...or If Not IsPostBack Then ... End If in VB.NET
you can try to use
DropDownList2.SelectedItem.Value
instead of
DropDownList2.SelectedItem.Text