Hide control in Page_Load - asp.net

I am using label control in form designing.But i want to hide that particular label control on page load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label10.Visible = False
End Sub
What should i do? I'm new to vb.net.

Try setting the visibility property within the aspx code rather than within the load event, this may solve your issue.
<asp:Label ID="lblValidation" runat="server" BackColor="Red"
Text="Please fill in all of the date fields below to proceed" Visible="False"></asp:Label>
Hope this helps!

Related

Find a control from an updatepanel and manipulate it

I have a .aspx page that contains a hidden panel that I need to display from a .ascx control.
On my .Click event from my button on my .ascx I have the following:
Dim myControl2 As Control = FindControl("Content1")
Dim myControl3 As Control = FindControl("keywordSearchModal")
On my .aspx I have the following:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentHolder" runat="server">
<asp:Panel ID="keywordSearchModal" runat="server" Width="800px" Visible="true">
This is your modal
<asp:Button ID="OKButton" runat="server" Text="Close" />
</asp:Panel>
</asp:Content>
Both myControl2 and myControl3 contain nothing when the button is clicked.
How can I find this control from the .ascx that also happens to be an updatepanel.
UPDATE
I was able to find the control by doing the following:
Dim myPanel As Panel = Page.Master.FindControl("ContentHolder").FindControl("keywordSearchModal")
myPanel.Visible = False
Dim myControl As Control = Page.Master.FindControl("ContentHolder").FindControl("keywordSearchModal")
myControl.Visible = False
I was able to use the ContentPlaceHolderID - However now I can't change the visibility...
UPDATE II
thanks to the suggestion below I was able to capture the button click on my .aspx page. However, now just like before I can't seem to manipulate the control. In this example adding text to the panel.
Protected Sub SearchStart(ByVal sender As Object, ByVal e As EventArgs) Handles EquipmentDetails1.SearchStart
Dim newLabel As New Label
newLabel.Text = "This is a label!"
keywordSearchModal.Controls.Add(newLabel)
End Sub
Thoughts?
Here is what I've used in the past...
My user control has an imagebutton and the following in the codebehind
Public Event SearchStart As EventHandler
'This will be the event triggered when the imagebutton is clicked
Protected Sub ib_Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ib_Search.Click
'Bubble up event to parent
RaiseEvent SearchStart(Me, e)
End Sub
Any page that uses the User Control will have the following in codebehind...
Protected Sub mc_SearchFilter_SearchStart(ByVal sender As Object, ByVal e As EventArgs) Handles mc_SearchFilter.SearchStart
'CODE that needs to be executed when search image button is clicked in my_control_SearchFilter
End Sub
Hope that helps....

Is it possible to disable an ImageButton when processing?

Is it possible to disable an ImageButton when running a process?
I found a way to disable a Button when processing, doing this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim strProcessScript As String = "this.value='Processing...';this.disabled=true;"
btnProcess.Attributes.Add("onclick", (strProcessScript + ClientScript.GetPostBackEventReference(btnProcess, "").ToString))
End If
End Sub
But it didn't work for ImageButton.
Also, I tried this code for disabling an Imagebutton:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim strProcessScript As String = "this.value='Processing...';this.disabled=true;"
btnProcess.Attributes.Add("onclick", (strProcessScript + ClientScript.GetPostBackEventReference(btnProcess, "").ToString) + ";return false;")
End If
End Sub
Neither of both ones makes ImageButton to be disabled when processing, but it works for Button.
this suits better for full postback solution, if you are using ajax, I'd recommend to consider a jquery plugin or any other javascript plugin you can search for, saying that well hereĀ“s a small work around:
you need to use the onClientClick event that's provided OOB for the image button, it's easier to manage due to it's directly tied to the onlick event over the client and there's no nee to registar a custom client event when clicking the button.
all you need to do is passing your code to the onClientClick event
<asp:ImageButton runat="server" ID="BtnSubmit"
OnClientClick="this.disabled = true; this.value = 'Processing...';"
UseSubmitBehavior="false"
OnClick="BtnSubmit_Click"
Text="Submit Me!" />
take a look to this solution

Radio button checked changed event in asp.net

I have 2 radio buttons on a simple web page and If I click one of them the other should be false(i.e checked=false) but it's not working Can anyone point me the mistake I'm doing.I knew it's a silly one but I need to know what's going on?
Here are the radio buttons:
<asp:RadioButton ID="Rb1" runat="server" Text=""/>
<asp:RadioButton ID="Rb2" runat="server" Text=""/>
Onchecked changed event:
Protected Sub Rb1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rb1.CheckedChanged
Rb2.Checked = False
End Sub
Protected Sub Rb2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rb2.CheckedChanged
Rb1.Checked = False
End Sub
You have to set GroupName property and AutoPostBack=True.

asp.net CustomValidator fires, but ServerValidateEventArgs Value always empty

I'm having a problem implementing a CustomValidator, I have multiple TextBoxes with a MaskedEditExtender, they all should contain a date ("dd-MM-yyyy"). To check this date I want to use the CustomValidator, but the e.Value passed to my MyValidate function is always empty, while the TextBox is not.
code:
<asp:TextBox ID="Gereed" runat="server" CssClass="date" />
<asp:CustomValidator ID="cd1" runat="server" TargetControlID="Gereed" />
<asp:MaskedEditExtender ID="md1" runat="server" TargetControlID="Gereed"
Mask="99-99-9999" ClearMaskOnLostFocus="false"/>
code behind:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
cd1.ValidateEmptyText = True
AddHandler cd1.ServerValidate, AddressOf ValidateDate
End Sub
Protected Sub ValidateDate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
e.IsValid = MyValidate(e.Value, "dd-MM-yyyy")
End Sub
I had a ClientValidationFunction that has the same problem.
Does anyone know a solution to this? I guess I'm missing something, but I don't know what, a similar solution in another website works perfectly.
TargetControlID is not a property of CustomValidator, it should be ControlToValidate. Somehow there was no errormessage, normal Validators throw an exception if ControlToValidate was not found, but the CustomValidator does not.

Addhandler, button.click not firing using VB.NET

I am experiencing a problem with buttons and AddHandler. It only works if I use AddHandler Button1.click, AddressOf... in Page_load, but if I create the button dynamically in one of the sub routines, the event doesn't fire.
For example,
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
</asp:DropDownList>
<asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
<contenttemplate>
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
</contenttemplate>
</asp:UpdatePanel>
<asp:UpdatePanel id="UpdatePanel2" runat="server" UpdateMode="Conditional">
<contenttemplate>
<asp:Label id="Label2" runat="server" Text="Label"></asp:Label>
</contenttemplate>
</asp:UpdatePanel>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Label1.Text = Date.Now
ScriptManager1.RegisterAsyncPostBackControl(DropDownList1)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Label2.Text = "Panel refreshed at " + Date.Now.ToString()
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim b As New Button
b.Text = "Click"
ScriptManager1.RegisterAsyncPostBackControl(b)
AddHandler b.Click, AddressOf Button1_Click
PlaceHolder1.Controls.Add(b)
UpdatePanel1.Update()
End Sub
The dropdownlist works, but the button doesn't. What am I doing wrong?
You have to regenerate your dynamically created controls on every postback (at last in Page_Load, better in Page_Init). You have to set the ID of the controls accordingly because ASP.Net needs it to identify which control caused a Postback and to handle the appropriate events.
You could save the number of created buttons in ViewState and use this to regenerate them on Page_Load. Increase the number when you add a new button. Use this number also to make the Button's ID unique(append it to the ID) to ensure that its the same on every postback.
For further informations, have a look the Page-Lifecycle and ViewState with dynamically added controls.
Edit: As Joel commented, if you only need one Button you can set it's ID statically, but you have to regenerate it on postback f.e. to handle its click-event.
Just to aid anyone who has this problem and isn't quite sure how to implement. Here's a quick example.
This example starts out by displaying a dropdownlist. When user selects something from the dropdown, another dropdownlist appears.
I typed this off the top of my head, so it MAY contain errors, but you get the idea =)
In the aspx file, add a placeholder:
And in your codebehind:
...
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'Store control count in viewstate
If Not IsPostBack Then ViewState("ControlCounter") = 1
'Rebuild dynamic controls on every postback so when page's life cycle hits Page_Load,
'selected values in the viewstate (asp.net default behavior) can be loaded into the dropdowns
Build_Dynamic_Controls()
End Sub
Protected Sub Build_Dynamic_Controls()
'Clear placeholder
myPlaceholder.Controls.Clear()
'This is where the control counter stored in the viewstate comes into play
For i as Integer = 0 To CInt(ViewState("ControlCounter") -1
Dim ddlDynamic as New DropDownList With {
.ID = "ddlDynamicDropdown" & i,
.AutoPostBack = True
}
'This is the event that will be executed when the user changes a value on the form
'and the postback occurs
AddHandler ddlDynamic.SelectedIndexChanged, AddressOf ddlDynamic_SelectedIndexChanged
'Add control to the placeholder
myPlaceholder.Controls.Add(ddl)
'Put some values into the dropdown
ddlDynamic.Items.Add("Value1")
ddlDynamic.Items.Add("Value2")
ddlDynamic.Items.Add("Value3")
Next i
End Sub
Protected Sub ddlDynamic_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'When a dropdown value is changed, a postback is triggered (autopostback=true)
'The event is captured here and we add another dropdown.
'First we up the control count:
ViewState("ControlCounter") = CInt(ViewState("ControlCounter")) + 1
'Now that the "total controls counter" is upped by one,
'Let's recreate the controls in the placeholder to include the new dropdown
Build_Dynamic_Controls()
End Sub
...

Resources