I've got a GridView like below:
<asp:GridView ID="Results" runat="server" OnRowDataBound="Results_RowDataBound">
<EmptyDataTemplate>No results found</EmptyDataTemplate>
</asp:GridView>
Protected Sub Results_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
'do a bunch of work here
End Sub
Based upon user input, sometimes I want the OnRowDataBound event to fire, sometimes I don't.
Is there a way to programatically turn the event on or off?
Here is a sample code to add and remove events in VB.NET programatically :
If CheckBox1.Checked Then
AddHandler Results.RowDataBound, AddressOf Results_RowDataBound
Else
RemoveHandler Results.RowDataBound, AddressOf Results_RowDataBound
End If
Wouldn't it be easier to add an if inside your event handler and ignore the event when you don't need it?
Related
How do I set ListView data through the codebehind instead of using the Bind() function in the Text attribute?
Right now I'm doing the following, but I'd like to have it retrieved and set in the codebehind. I'm using VB... Thanks!
<asp:Label ID="Date" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
Edit:
Sorry, I'm binding the data in the following way with a DataTable.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ListView.DataSource = MyDataTable
ListView.DataBind()
End If
End Sub
use the ItemDataBound event.
Without seeing your code, I can tell you that a ListView has a DataSource property that you should just be able to set in your load code (and then do a DataBind()). I know I've done that before with a GridView.
Based on the info you have provided this is the best I can give you. You may want to put this snippet in the PreRender event for your ListView.
Label lblDate = (Label)ListView.FindControl("Date");
if(dataTable.Rows.Count > 0 && dataTables.Columns.Contains("Date"))
{
DataRow row = dataTable.Rows[0];
If(!DBNull.Equals(row["Date"])
{
lblDate.Text = row["Date"].ToString();
}
}
What we want to do is save an entire control into Cache and recommit the properties on page load, including data items. But we'd like the controls to exist already in the page.
Is this possible?
<html>
<asp:Repeater runat="server" id="rptListOfSubscribers">
<ItemTemplate>
<%# Eval("Name")%><br />
</ItemTemplate>
</asp:Repeater>
</html>
VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Cache("MyRepeater") Is Nothing Then
Dim rpt As Repeater = InitaliseRepeater()
Cache.Insert("MyRepeater"), rpt, Nothing, DateTime.Now.AddMinutes(720), Tim
eSpan.Zero)
End If
rptListOfSubscribers = Cache("MyRepeater")
End Sub
Function InitaliseRepeater() As Repeater
Dim rpt As New Repeater
rpt.DataSource = x
rpt.DataBind()
Return rpt
End Function
Excuse the short-hand code.
Yes that is possible the cache will take any object including a webcontrol. However probably best to do it earlier than Page_Load -Page_init perhaps.
Also I am curious as to your reasons behind doing this .. It seems like an awful solution. Also when you add the control to the page again when the viewstate page cycle methods run it will potentially change the control instance you have...
Can you describe the problem you are trying to solve with the cached web control?
I'm still somewhat new to ASP, and can't seem to figure out what the issue is.
When I update an item in FormView, I need the text of a button to change.
I'm using:
Protected Sub fvClientInfo_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles fvClientInfo.ItemUpdated
btnEditClient.Text = "Edit"
End Sub
The line of code is being called (can tell from debug mode), but the button text isn't changing and I have no idea why.
I should make sure to mention that the button is NOT inside the FormView.
Any ideas?
Try this.
Protected Sub btnchange() Handles FormView1.DataBound
Button1.Text = "Newtxt"
End Sub
and add this to your formview
OnDataBound="btnchange"
I have a system setup as follows:
User Control Called "Main" when I click a button in "Main" another user control (user control "A", user control "B", user control "C") is populated inside a placeholder of "Main" depending on some logic either A,B, or C is populated.
User controls A,B,C have many buttons on them as well. They Also have a placeholder to contain either user control x, user control y, user control z depending what is clicked inside user control "A" for example.
Main loads controls (A,B, or C) into its placeholder no problem, but when I click a button in A,B,or C to load their placeholder with x,y, or z the whole control (A,B, or C) dissappears.
I understand this has to do with viewstate not holding dynamic controls during a postback. So what I did was explictly called viewstate on the controls loaded into main (A,B,C). when they are loaded I save an entry of what was loaded like this ViewState("lastLoaded")='A' for example. When a postabck occurs (i.e. clicking a button in A) I reload the whole control A.
This is what happens:
I click a button in user control "A"
Postback occurs
User control "A" is reloaded because of the viewstate("lastloaded")
Then I have to click the button in "A" again at that time the button_click event fires
Can someone please help me fix this.
'Here is Main.ascx
Partial Class Main
Inherits System.Web.UI.UserControl
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
ViewState("name") = "ControlA2.ascx"
AddControl(ViewState("name").ToString())
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If ViewState("name") <> String.Empty Then
AddControl(ViewState("name").ToString())
End If
End Sub
Public Sub AddControl(ByVal name As String)
PlaceHolder1.Controls.Clear()
Dim toAdd As Control = LoadControl(name)
PlaceHolder1.Controls.Add(toAdd)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
ViewState("name") = "ControlB.ascx"
AddControl(ViewState("name").ToString())
End Sub
End Class
'Here is Main designer
Main
asp:Button ID="Button1" runat="server" Text="Add Control A"
asp:Button ID="Button2" runat="server" Text="Add Control B"
asp:PlaceHolder ID="PlaceHolder1" runat="server">
'Here is ControlA2.ascx
Partial Class ControlA2
Inherits System.Web.UI.UserControl
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
PlaceHolder1.Controls.Clear()
Dim toAdd As Control = LoadControl("ControlX.ascx")
PlaceHolder1.Controls.Add(toAdd)
End Sub
End Class
'Here is ControlA2 designer
I am Control A
Add Control X
asp:Button ID="Button1" runat="server" Text="Add Control X"
asp:PlaceHolder ID="PlaceHolder1" runat="server"
/asp:PlaceHolder
'Here is ControlB.ascx
Partial Class ControlA2
Inherits System.Web.UI.UserControl
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
PlaceHolder1.Controls.Clear()
Dim toAdd As Control = LoadControl("ControlZ.ascx")
PlaceHolder1.Controls.Add(toAdd)
End Sub
End Class
'Here is ControlB designer
I am Control B
Add Control Z
asp:Button ID="Button1" runat="server" Text="Add Control Z"
asp:PlaceHolder ID="PlaceHolder1" runat="server" /asp:PlaceHolder
'Here is ControlX.designer
asp:Label ID="Label1" runat="server" Text="Label">I AM CONTROL X /asp:Label
'Here is ControlZ designer
asp:Label ID="Label1" runat="server" Text="Label">I AM CONTROL Z /asp:Label
I've created a project based on your code and I've managed to recreate the issue.
When I initially run it and click on the Add control A button the Main control will load Control A and note this in the viewstate. Control A's button to add Control X will now appear with an id of Main1_ctl00_Button1. When clicking on this button Main's page load will add Control A back to it's placeholder and Control A's button click event will fire. If you now click Main's Control A button again the problem arises. Main's page load method will look at the Viewstate and add control A to the placeholder. Then Main's button click event will then fire clearing the placeholder and re-adding control A. When the form is displayed Control A's button to add Control X will now have an id of Main1_ctl01_Button1 as it was the second control to be generated in the code behind. Now when you click on the button to add control x Main's page load will add Control A again but as it's id will be Main1_ctl00_Button1 and the click event came from a button with an id of Main1_ctl01_Button1 the event will not fire.
To fix this you will have to avoid the duplication of control creation the second time around. You could do this by checking the requests form collection during the page load to see which button was pressed although this is not a very elegant solution. You will need to ensure that the buttons on the Main control have a unique text value for this to work.
Hopefully someone else may be able to come up with a more elegant solution:
Dim mainButtonClick = False
For index As Integer = 0 To Request.Form.Count
If Request.Form(index) = "Button A" Or Request.Form(index) = "Button B" Then
mainButtonClick = True
End If
Next
If Not IsPostBack And Not mainButtonClick And ViewState("name") <> String.Empty Then
AddControl(ViewState("name").ToString())
End If
Please excuse any errors in this code as I am not a VB.NET programmer.
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