DevExpress AspxGridView GetRowValues during BeforePerformDataSelect Event - asp.net

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")

Related

Making combobox_selectedIndexChanged work

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.

Get XPath data for a bound repeater

I have a Repeater element in my .aspx file that calls a bind sub in my CodeBehind on my OnItemDataBound event, similar to this:
Sub SomeRepeaterBinder(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
(code binding data to repeater form objects goes here)
:
(etc.)
For the most part, it works great, but here's the problem I'm trying to solve: if there is no data, I want it to either (1) hide the repeater item, or (2) exit the sub completely (or maybe both).
In order to do that, I need to know whether or not any data exists in my Repeater item. And despite reading loads of documentation about RepeaterItemEventArgs properties (and running lots of debuggers), I haven't been able to figure out the correct syntax.
Can someone help me out with this? I'm hoping this is an easy question with an easy answer.
Note: my data source is XML. My form objects and my Repeater data source have XPaths assigned to them. I'm working with VB.
Thanks in advance . . .
I don't know if this is the best answer for this, but here's how I got it to work.
I didn't use e.Item.DataItem at all. I took the XML data source for my repeater and used selectSingleNode (using the data source XPath) to return my value. If the resulting string is empty, I hide the row.
However, if anyone has a more elegant or robust answer, I'm all ears.

Single event handler for multiple links/buttons on ASP.NET

I have a dropdown list that contains a collection of names. My entire names list is very large (over 2,000) so I would like to pair down the names in the drop down list to those starting with the same letter.
To do this I would like to have 26 links all on the same line, one for each letter in the alphabet ..
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
The idea being that the user clicks on the letter they are interested in and the stored procedure that obtains the list of names is re-executed to only grab those names starting with the letter that was clicked and then the resulting dataset is rebound to the dropdown list.
What is vexing me is how to handle creating all the "Click Events" necessary to deal with the user "clicking" on a link. I could create 26 different event handlers, one for each link, but I have to believe there is a simpler way I am not seeing.
Form demonstration here is the click event for one link, the letter "A" ...
Protected Sub lnkLetterA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkLeterA.Click
Call LoadNamesIntoDropDown("A")
End Sub
Is there a way to create one event handler that could handle all 26 links? Thank you.
P.S. C# or VB.NET examples are fine. I just happen to be using VB.NET in this case.
You can reuse the same click handler a simple example
protected void HandleLinkClick(object sender, EventArgs e)
{
HyperLink link = (HyperLink)sender;
LoadNamesIntoDropDown(link.Text);
}
However, there are loads of autocomplete style solutions you can use. A free one from MS
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/autocomplete/autocomplete.aspx
Of course you can have one handler to rule them all. Just connect the Click event of all the links to the same method.
Do you create the links dynamically in code-behind, or have you created them in the designer? If it is done in the designer:
Select a link
In the property grid, switch to the event view
In the click event, select your event handler from the dropdown list
Repeat for all links
In the event handler, use the sender argument to examine which of the links that was clicked, and act accordingly.
As per your example use:
Protected Sub lnkLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkLeterA.Click, lnkLeterB.Click, lnkLeterC.Click //etc
Call LoadNamesIntoDropDown(CType(sender, LinkLabel).Text)
End Sub

Why is the DataBind() method necessary?

Simple question, I guess.
For a long time I've blindly followed a (supposedly) common pattern when programmatically databinding my ASP.NET controls. Namely:
gridView1.DataSource = someList;
gridView1.DataBind();
However, if I were setting my GridView to bind to a DataSource control via the DataSourceID property, the call to DataBind() is unnecessary. Namely:
gridView1.DataSourceID = LinqDataSource1;
is sufficient.
Furthermore, if you try to set the DataSource property in ASPX markup, you are greeted with the following:
You cannot set the DataSource property declaratively.
I assume these are related, but I am still stumped as to why DataBind() is necessary. The difference between DataSource and DataSourceID is secondary - I can understand some magic taking place there. The real question is why doesn't the DataSource propery setter cause databinding automatically? Are there any scenarios in which we want to set the DataSource but not bind to it?
In ASP.Net, it's often important to have certain data available and ready at certain points in the page life cycle, and not before. For example, you may need to bind to a drop down list early to allow setting the selected index on that list later. Or you might want to wait a bit to bind that large grid to reduce the amount of time you hold that connection active/keep the data in memory.
Having you explicitly call the .DataBind() method makes it possible to support scenarios at both ends of the spectrum.
DataSource is a property of the BaseDataBoundControl class. DataSourceID is a property of the DataBoundControl class, which inherits from BaseDataBoundControl and did not exist before ASP.NET 2.0.
Since DataBoundControl is explicitly for displaying data in a list or tabular form, and BaseDataBoundControl cannot make that assumption, binding is not automatic when DataSource is set because the type of control may not match the structure of the data.
Of course, this is all just a guess based on the MSDN documentation, so I could be wrong.
I noticed that without using DataBind() that nothing will be displayed in my GridView so I always include it as shown in this section of code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' TableAdapter object.
' Provide communication between this application and the database.
'-----------------------------------------------------------------
Dim suppliersAdapter As New SuppliersTableAdapter
' Get the data from the TableAdapter into the GridView.
'------------------------------------------------------
GridView1.DataSource = suppliersAdapter.GetSuppliers()
' Display the result set from the TableAdapter in the GridView.
'--------------------------------------------------------------
GridView1.DataBind()
End Sub
Please forgive the extra commenting as I'm also still learning ASP.Net as well and the comments will help me learn better "what and why" to use certain statements.
Try this:
if (GridView1.EditIndex == e.Row.RowIndex)
{
TextBox t2 = (TextBox)e.Row.FindControl("TextBox2");
DateTime dt2;
if (DateTime.TryParse(t2.Text, out dt2))
{
t2.Text = dt2.ToString("yyyy-MM-dd");
}
}

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

Resources