The page I have pulls information from the database and based off that it will either generate a text box or radio button on the page load. The issue I am running into is that I am not able to utilize it later in the code behind. I am wondering if its possible and how to make these accessible. For example, TextBox1 is created during the page load, then on a button click the code below will throw the error "'TextBox1' is not declared."
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.text = TextBox1.text
End Sub
Best practice would be to put the controls in the ASPX markup. That allows you to reference it from code behind everywhere; however, if that is not an option for you and you need to handle everything in code:
Dim control as Control
Protected Sub Load()
If ConditionForRadioButton Then
control = new RadioButton()
End If
End Sub
Protected Sub Button1_Click()
If ConditionForRadioButton Then
Dim radio = CType(control, RadioButton)
' do things with radio
End If
End Sub
Add other cases for the other control types; if this is in some kind of collection you'd have to extrapolate this to work in that case, but the idea would be the same.
Related
I have many image buttons on my page based on asp.net, I want to handle the click event of all in a single sub..
The reason for this is; the number of image buttons can increase or decrease by programming...
So i want something like this...
Private sub , (this sub must handle all the image buttons on the page)
My code come here
End sub
Now user clicks whichever image button, this sub should be fire.
One Private Sub handles multiple image button
Private Sub imagebtn_Click(sender As Object, e As EventArgs) Handles imagbtn1.Click, imagbtn2.Click, imagbtn3.Click, imagbtn4.Click, and so on
'code here
End Sub
I have simple VB.NET that contains two database driven dropdownlists. Each dropdownlist has an autopostback property that will execute a function that takes user to another page.
The problem I have is that if user selects dropdownlist A, is taken to the A page, but then presses back button and selects dropdownlist B, the autopostback will occur for dropdownlist A since dropdownlist A is still selected. Any ideas on how to get around this?
I've tried everything I can think of. I do reset the dropdownlist and comment out the Response.Redirect code to prove that it does actually reset. But then as soon as I put the redirect back in, it loses the reset to unselected ability. I've seen this "bug" posted elsewhere online, but have not found a solution that works.
Protected Sub ddlSearchAward_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSearchAward.SelectedIndexChanged
Dim strParam As String
strParam = ddlSearchAward.SelectedItem.Value
Response.Redirect("awards_?criteria=" & strParam)
End Sub
Protected Sub ddlAwardList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlAwardList.SelectedIndexChanged
Dim strParam As String
strParam = ddlAwardList.SelectedItem.Value
Response.Redirect("awards_?id=" & strParam)
End Sub
I had to forgo the autopostback and put a button next to each dropdownlist to get it to work.
Situation:
I have created a custom webusercontrol. The user control is fully created in code .
It consists of a radgrid and radcombox.
The radcombox is filled with database tables, when the user select a table the data
needs to be shown in the radgrid.
This usercontrol is added to a aspx page. Everything works perfectly.
Here comes the problem:
I want a radajaxloadingpanel to be shown on the radgrid while it rebinds, using a radajaxmanagerproxy control. This doesn't work until I hit the refresh button or a page next or prev of the radgrid. Then everything works fine.
I know the combobox works because the data does change only the loadingpanel isn't shown.
Does anybody have a clue on how to solve this, a work around or a suggestion I can look into?
Finally got it to work. I did something wrong in the creation life cicle:
now i have it like this. And it works ;)
public class CustumControl
implements CompositeControl
Private RadAjaxLoadingPanel1 As New RadAjaxLoadingPanel
Private RadAjaxManagerProxy1 As New RadAjaxManagerProxy
....
Protected Sub Control_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.Controls.Add(RadAjaxLoadingPanel1)
Me.Controls.Add(RadAjaxManagerProxy1)
With RadAjaxManagerProxy1.AjaxSettings
.AddAjaxSetting(...)
End with
End Sub
....
Protected Overrides Sub CreateChildControls()
'Set properties controls and add themwill
End Sub
end class
Hope this will help other people with simular problems.
grtz Yuri
I know the title is vague, so I'll fully explain here my problem..
So, I'm running VB.net 3.5. I have a dynamic list of server names, and I want to put them in a CheckBoxList. The list is populated and, using that same list, I make a graph of the performance for each server listed. I want to be able to check and uncheck the checkboxes representing servers and, when I lick an update button, it'll create a new graph and graph only the servers that are still checked. I noticed that the page still loads before the button click is handled, so the CheckBoxList will repopulate itself before being able to read the current CheckBoxList. Does anybody have any input on this?
This is my load. And I populate my checkboxlist inside ShowView()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If _myQSVar.Count = 0 Then
Calendar1.SelectedDate = Date.Now.Date.AddMonths(-6)
Calendar2.SelectedDate = Date.Now.Date
End If
tbFromDate.Text = Calendar1.SelectedDate.ToShortDateString()
tbToDate.Text = Calendar2.SelectedDate.ToShortDateString()
lstControls = New List(Of System.Web.UI.Control)
ShowView()
End If
End Sub
I load my checkboxlist with a simple for loop
For each one As String in ServerList
chkboxList.Items.Add(one)
Next
And I wanna try to preserve the checkboxlist values when I do an event handler for an update button.
Private Sub btnUpdateGraph_Click(sender As Object, e As System.EventArgs) Handles btnUpdateGraph.Click
'insert code
End Sub
Are you populating the checkboxlist on form.load? If so make sure you checking for if Page.ispostback. Load the checkboxlist only if its not a postback. This will make sure the page(and the checkboxlist) is not reloaded when you click.
Also, please do not lick update buttons. They dont really like that!
I believe you are binding CheckboxList at PageLoad Event. Just place the code inside the given code block:
if(!IsPostback)
{
// Bind your Checkbox List here
}
This will bind your checkboxlist only in case if the page loads first time, not after any postback.
I hope this will help you out.
So i have a form and one of the fields may include one item or may require more fields to be created immediately to accommodate the extra input. so something like below:
FieldName:------ +
The plus sign would be clicked to get another field:
FieldName:------ +
FieldName2:------ +
.... so on
Sorry for poor illustration. I am looking to do this in an asp.net page. any idea how? need ajax component?
Thanks,
EDIT:
Protected Sub Button1_Click( ByVal sender As Object , ByVal e As System.EventArgs) _
Handles Button1.Click
Dim t As TextBox
t = New TextBox
PlaceHolder1.Controls.Add(t)
End Sub
This code would add one textfield. But if I want to add more, i am unable to do so...
You could create a Placeholder control and onclick (postback) of the '+' button add a new control (e.g. Textbox) to the Controls collection of the Placeholder.
You would need to remember to re-add any controls to the placeholder upon subsequent postbacks (within the OnInit() preferably). This is so that previously added controls and their values can be retained.