Making combobox_selectedIndexChanged work - asp.net

I have a web page that on page load loads data into drop down lists and the user has the option to change the values if they wish too. How do I make it so everytime they change the value it will save it and resend it to the database? So far all I have is this:
Private Sub cboCure_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCure.SelectedIndexChanged
cboCure.SelectedItem.Text = CStr(sender)
...database functions using cboCure.SelectedItem.Text
End Sub
I don't know if this is enough information to help out at all, if it's not just lemme know... I don't really know what else to put in this one.

Set autopostback property of your combobox to true. So that, whenever a change happens; you can check if if(ispostback) and have your code to do the insertion of changed data in DB.

Related

DevExpress AspxGridView GetRowValues during BeforePerformDataSelect Event

So, I have a master grid and a detail grid within that master grid.
I am grabbing the masterkey from my parent grid via the BeforePerformDataSelect and placing this value into a session variable. At that point I also need to grab a value from the specific row that I am on. Lets call that variable SENT_DATE.
Here is some sample code.
Protected Sub gvDetails_BeforePerformDataSelect(ByVal sender As Object, ByVal e As EventArgs)
Dim gvDetails As ASPxGridView = (TryCast(sender, ASPxGridView))
Session("ID_NUMBER") = gvDetails.GetMasterRowKeyValue
Session("SENT_DATE") = gvDetails.GetRowValues("SENT_DATE")
End Sub
I have worked with DevExpress products a lot before but it has been quite some time. If I remember correctly, normally I just grab the e.VisibleIndex and I am able to go from there but in this specific event I am unable to grab this. I know the above code is incorrect for grabbing the variable SENT_DATE, but I am not sure what to do here.
Any ideas, advice would be greatly appreciated. Thanks. I have searched the DevExpress forums deeply.
You can use FocusedRowIndex property.
Session("SENT_DATE") = gvDetails.GetRowValues(gvDetails.FocusedRowIndex, "SENT_DATE")

How to register data for validation using RegisterForEventValidation in asp.net

I would like to know the method of using RegisterForEventValidation in asp.net
My problem is this.
If I enable eventvalidation, then changing the controls using javascript and then posting the information back to the server later on throws up an error.
But If I disable event validation, the data present/selected in the controls is not available in the event handlers in code behind.
So, how should one resolve such issues?
Also, are there are any good articles that explain the issue and a resolution in detail? Tried googling. Came across many articles. But nothing that matched my expectations.
A small progress.
If I do the below, the event validation error goes away, but am not able to get the selected value in the code behind on button click (after selecting "1" in the dropdown, since for now I have registered only that value)
I get a runtime error - Unable to convert from string to double when I try accessing the selected value in drop down in code behind (The reason I believe is that no value is passed in the first place).
Any idea on what might be going wrong here!? Thanks!
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Page.ClientScript.RegisterForEventValidation(Me.ddldobddId.UniqueID, "1")
MyBase.Render(writer)
End Sub
Protected Sub btnId_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnId.Click
If CType(Me.ddldobddId.SelectedValue, Integer) = 0 -> Throws the error
End Sub
You've pretty much got it. The DropDownList.SelectedValue is always going to be a System.String type.
You must convert the String to an Integer in your btnId_Click method.
Protected Sub btnId_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnId.Click
Dim convertedSelectedValue As Integer
convertedSelectedValue = Convert.ToInt32( Me.ddldobddId.SelectedValue )
End Sub

How do I make a Wizard control save the data after each step?

Is there a way that i can save data on each step in wizard control.
I want to save data when user clicks next button on each step.
I would like to save them to database , so that i can retrieve them back if user has opted to close and complete steps later without clicking finish button
In your code-behind, you can capture the "Active Step Changed" event and do whatever you want:
Protected Sub AddEmployeeWizard_ActiveStepChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddEmployeeWizard.ActiveStepChanged
'save your data here
End Sub
If you just want to save on a click of the Next button, you could instead do
Protected Sub myWizard_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles myWizard.NextButtonClick
'save your data here
End Su
b
You can save data to Session or ViewState objects.
Also you can add you saving logic in wizard events: ActiveStepChanged, CancelButtonClick, FinishButtonClick, NextButtonClick, PreviousButtonClick, SideBarButtonClick.
In ASP.NET you can probably stick it in the Session variable.
In an WPF or Winforms app, you could just put it in a variable in memory, and if these are settings for your program, you could save them to an XML file.

Add Multiple User Control of the Same Type to a Page

Similar questions to this one have been asked but none seem to address my exact situation here's what I am trying to do.
I have a user control that manages student info. i.e. FirstName, LastName, Address etc.
I have a webpage/form that has a button on it. "Add Student". What I want to accomplish is for a new StudentInfo control to be added to the webform after each click.
My current code looks something like this
Private Sub btnAddStudent_Click(sender as object, ByVal e As System.EventArgs)
Dim lStudentInfo as Control
LoadControl("~/StudentInfo.ascx")
Me.placeholder1.controls.add(lStudentInfo)
End Sub
With this code only one StudentInfo control is added and upon pressing the "Add" button again a new StudentInfo control isnt added below the first one and the text/data entered within the first control is cleared.
Thanks in advance for any assistance.
What is happening is that every time you do a postback your previous control was lost. Remember, every postback uses a brand new instance of your page class. The instance you added the control to last time was destroyed as soon as the http request finished — possibly before the browser even finished loading it's DOM.
If you want a control to exist for every postback you have to add it on every postback.
Additionally, if you want ViewState to work for the control you need to add it before the Load event for the page. This means either on Init or PreInit.
Private Sub btnAddStudent_Click(sender as object, ByVal e As System.EventArgs)
Me.placeholder1.controls.add(LoadControl("~/StudentInfo.ascx"))
Session("NewStudentControls") += 1
End Sub
Protected Sub Page_Init(sender as object, e as system.eventargs)
For i As Integer = 1 To Session("NewStudentControls")
Me.placeholder1.controls.add(LoadControl("~/StudentInfo.ascx"))
Next
End Sub

How can I share a variable from sub routine to sub routine?

I have a sub routine where I pull in information from a database. I want to use variables from this sub routine in another sub routine. I tried making everything public but it doesn't want to be friendly and share.
Dim strEmail as String
Public Sub readDB()
strEmail = "whatever#doohikcy.com"
End Sub
Public Sub submit_btn(ByVal sender As Object, ByVal e As EventArgs)
Response.Write(strEmail)
End Sub
The readDB would read the database and set the variables and do whatever it needs to do. Then when they submit the form it would e-mail the form to whatever the email address was.
Every time an asp.net page loads, the global variables are wiped out. You can avoid this by using Session variables.
Public Sub readDB()
Session("strEmail") = "whatever#doohikcy.com"
End Sub
Then:
Public Sub submit_btn(ByVal sender As Object, ByVal e As EventArgs)
Response.Write(CStr(Session("strEmail")))
End Sub
Passing them as arguments is great, if the sequence of calls to the subs in question makes that feasible. If you literally need a variable that is visible to different subs in the same class (code-behind page in ASP.Net), what you are looking for is probably a private member variable. Declare it outside of any sub or function with the access modifier Private, and all the subs in the class will be able to access it.
Private _foo As String
The underscore is a convention that some people love, some hate. It comes in handy in VB if you want to define a property to expose the variable, where you can't use Foo as distinctly different from foo, but that's another story.
This is not the same as what would generally be understood by the term Global variable in the ASP.Net sense, where the variable would be visible throughout the application context, which lends itself to unintended consequences in the least. A private member variable is only visible to the class that owns it.
EDIT: Your example code was added after my initial answer. My VB is a little rusty but as you've written it, strEmail looks like it should have class-level visibility, including inside of submitbtn (someone correct me if I'm wrong). One possibility, since you mentioned that you are calling readDB in Page_Load is if you're checking for postback in page load, and only calling readDB on the initial load, not on postback, which would be the case when the button is clicked. You may have seen examples that include a check for Postback out of hand and not realized what it does (I only suggest that because you mentioned that you are new to ASP.Net and it's not intuitive if you're new to it--no offense intended).
Protected Sub Page_Load (sender as object, e as EventArgs)
If Not IsPostback
// this doesn't get called when the button is clicked so
// strEmail would not be populated when submitbtn is invoked
readDB
End If
End Sub
That's a bit of a guess out of nowhere though, so it might be way off base. Have you set break-points in Page_Load, readDB and submitbtn to see the state of strEmail in each?

Resources