Pre-Select Items in a dynamically populated CheckBoxList - asp.net

I've been experimenting with this and trying to understand the Page Life Cycle for hours upon hours. I'm completely stumped at this point :'( Any help would be GREATLY appreciated.
Below are my methods. My _PrefillWizard method actually works for all the controls on my page except for those which are dynamically populated. The CheckBoxLists are such because they are automatically generated based on selectable values in a table in my DB. I already have this control in my markup like so... how do I force this to load and be ready before running my other code?
<asp:CheckBoxList ID="MyLanguagesCBL" runat="server" RepeatDirection="Vertical"
RepeatColumns="4" Width="100%" DataSourceID="LanguagesSDS"
DataTextField="Language" DataValueField="ID">
</asp:CheckBoxList>
<asp:SqlDataSource ID="LanguagesSDS" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="select ID, [Language] from LanguagesListTable"
DataSourceMode="DataReader">
</asp:SqlDataSource>
Obviously these particular controls are not yet generated or on the page by the time my _PrefillWizard has been called, resulting in none of the CheckBoxes being pre-filled. How do I make them generate before my prefill method is called? Thanks ;)
The click event that kicks things off.
'On "Load" Button click, clears a Wizard control and Prefills it with the
' data from the clicked record based on the primary key stored in SessionState.
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView2.RowCommand
If e.CommandName = "Select" Then
Session.Add("ClickedPrimaryKey", GridView2.DataKeys(e.CommandArgument).Value.ToString)
'This is the main function which calls "_SetCheckBoxes" among others.
_PrefillWizard(Session.Item("ClickedPrimaryKey"))
End If
End Sub
Here's my function for populating the my CheckBoxList control, but it obviously depends on the control having already been loaded on the page.
'Parse a CSV String from the Database back into CheckBoxList Selections
Protected Sub _SetCheckBoxes(ByRef cbl As CheckBoxList, ByRef dt As DataTable, ByVal row As Integer, ByVal csv As String)
'Do something if there is a value in the cell
If Not IsDBNull(dt.Rows.Find(row)(csv)) Then
For Each item As ListItem In cbl.Items
item.Selected = False 'Reset the checkbox first
'Then, if the ID value of the checkbox corresponds to a value
' in the CSV string, then tick the checkbox.
If csv.Contains(item.Value) Then
item.Selected = True
End If
Next
End If
End Sub

Here is a potential idea, and one that we have used before in a similar situation:
Because the Page Init event is fired before the click handler, you can actually examine Request.Form("__EventTarget") and, if it is your row click, add the new CheckBoxList to the appropriate container (page, tab, etc).
If you are adding multiple controls, you will have to be sure that you track the added items somewhere in the page so that they can be automatically re-created each time Page Init is fired.

You guys will probably get a kick out of this, but the answer was so obvious that I didn't even catch it all this time. Of course I was preoccupied with about 10 other functions too >.<
Here's the answer. You'll notice that in my _SetCheckBoxes method, I first checked to ensure that a value existed for the particular csv string, but also notice, that I failed to actually SET it to the corresponding value string afterward. This isn't immediately apparent, but what I was actually passing in as csv was the name of the Column in my DataTable where the string actually lived.
csv = dt.Rows.Find(row)(csv).ToString
That line added immediately after my IsDBNull check, fixed my problem. :)

Related

how to use session in transferring id from one aspx page to another aspx page

I have two aspx pages. The first one contains the gridview list of all the vessels and the other one consists the application form for permit. Once i select a row in the gridview list,
a button for the permit application will be visible, this button once clicked, redirects to the second page which is the application form for permit. The problem is i have to get the id of the vessel from the selected row on the gridview list (first aspx page) and transfer it to the second aspx page. The id should be inserted in the permit application table together with the other data in the permit application form.
It sounds like this is your situation, roughly speaking: you have aspx pages A and B. Your user inputs something on A that is needed by B to render something else. To me the fact that you are rendering a table on B is irrelevant; if you could simply echo the value inputted from A onto B you would know what to do from there. So I am going to ignore the data table stuff.
You could use a session variable to transmit this value from A to B, but you probably shouldn't. There are a few good reasons not to do so; for starters, session variables are persisted in memory and therefore need to be managed.
If you insisted on using session variables, you would handle the event of, say, clicking submit on A by storing the text box's value into a session variable of your choice:
Public Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Session("my_session_variable_name") = myTextBox.Text
End Sub
And then in B, in the appropriate lifecycle event (say, on load) you would read the session variable and produce the table or whatever you are trying to accomplish:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Session("my_session_variable_name") <> "" Then
id = Session("my_session_variable_name")
End If
But I would highly discourage you from doing it this way. Instead, try to make your B page take in either a query string parameter or a POSTed body member, and read it using Request.QueryString[] or Request.Form[].
You should use CommandName and CommandArgument attributes of the Button.
<asp:Button runat="server" ID="btn" CommandName="Permit" CommandArgument="6" />
But CommandArgument must be bind OnRowDataBound event of the gridview. So when you click you will check command name and get command argument value and pass it to permit form.

How to avoid using Session

i have an interface that contains data-grid. When i add element to data-grid, i add it also into List property that is the data-source of my Data-grid. Here the declaration of my list in code-behind:
Public Property listeSpecialite() As List(Of RECSPECIALITECONCOURS)
Get
Return Session("specialite")
End Get
Set(ByVal value As List(Of RECSPECIALITECONCOURS))
Session("specialite") = value
End Set
End Property
and here is the code when i add element to Data Grid :
Protected Sub gridsecialite_ItemCommand(source As Object, e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles gridsecialite.ItemCommand
If e.CommandName = "Insert" Then
Dim dropSpecialite As DropDownList = CType(e.Item.FindControl("txtSpecialite_Footer"), DropDownList)
Dim specialite As New RECSPECIALITECONCOURS
specialite.CODESPECIALITE = IGS.ChercherParIdInt(Of GENSPECIALITE)(CInt(dropSpecialite.SelectedValue))
listeSpecialite.Add(specialite)
gridsecialite.DataSource = listeSpecialite
gridsecialite.DataBind()
End If
End Sub
and in user click save, i save all element in my list to database.
how can i save element of the list without the use of session. (my boss say that it's not good to store list of element in session-performance reason)
You can use ViewState to store listeSpecialite. Just make sure to understand how it works to ensure that match what you need.
Viewstate has its own drawbacks. More process time for serialization/deserialization, encoding/deocding in B64 and for the page lifecycle in load/restore viewstate of the page, etc. Also, by default, viewstate is sent to the client in a hidden field. This increase bandwidth.
Anyway, if your list has not to be persisted out of your current view I would use Viewstate.
EDIT:
With list of 1000 items viewstate will be hurge. How about read from data base persisted items and keep in viewstate just the added items?. You could retrieve items of database, retrieve items from viewstate, combine and bind to datagrid. It is another strategy to avoid session and hurge viewstate at the cost of read database every postback.
As I said, there is no silver bullet... :-P

DetailsView Insert default value for checkboxes

I have a big DetailsView form for an insert to a database and I have a lot (10+) checkbox fields. Many of them aren't visible because we don't want those fields activated yet to have data submitted (but may be activated in the future).
How I can set the default value of all checkboxes at first to "false" (unchecked)? As of right now, they are being inserted as nulls and that doesn't play too well with everything else.
So far I have this code behind running as an onDataBound event for the DetailsView:
Dim row As DetailsViewRow
For Each row In DetailsView1A.Rows
If row.Cells.GetType() = GetType(CheckBox) Then
Dim tempCheckbox As CheckBox = CType(row.Cells(1).Controls(0), CheckBox)
If tempCheckbox Is DBNull.Value Then
tempCheckbox.Checked = False
End If
End If
Next
However, all the checkboxes are still being submitted as nulls. What am I doing wrong?
Try handling the ItemInserting event, and updating the "Values" collection of the DetailsViewInsertEventArgs object. These are the values that will be passed to your datasource's insert statement.
Go through and update the "Values" entry for each of these hidden CheckBox columns to False. Here's is an example:
Sub CustomerDetail_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
e.Values("yourFieldName") = False
End Sub
Note that "yourFieldName" will be the name of the column in the datasource to which the DetailsView is connected.
Also, False might need to be a string, I'm not sure how this actually gets passed:
e.Values("yourFieldName") = "False"
One other thought: if the above doesn't work (for some reason those hidden CheckBoxes don't have entries in the Values collection), I would just not hide them on the server side - hide them using CSS or JavaScript. In other words, instead of having Visible="False" on your CheckBox, use the CSS property display:none.

PagedDataSource not working with previous/next pages

I'm having a hard time with this scenario and hoping someone can help me clarify where I'm missing a step/logic. I tried searching online but I couldn't find an example that addressed this issue.
We are setting up a search page that when first loads shows a bunch of options (e.g., textboxes, checkboxes, etc). The user will fill out the form and submit the form back to itself. Once posted back, the page will build and run a SQL query (e.g., SELECT ID FROM Customers WHERE Company = 'Acme' AND AmtDue = 3) against the database with the user's options and then the results will show up. This part works ok.
The part that breaks down is when I am trying to add pagination. The result set is a DataTable bound to a Repeater. I am using a PagedDataSource to add pagination. Pagination works great for the first page but for subsequent pages, it does not work. Basically, instead of returning the next page of the result requested (e.g., SELECT ID FROM Customers WHERE Company = 'Acme' AND AmtDue = 3), the results returned is the SQL query before appending the user's search options (e.g., SELECT ID FROM Customers).
I think my basic problem is that I'm not sure how to distinguish a normal Page.IsPostBack and paginating through results. The reason why this is causing problems is because I don't want to be regathering form data, rebuilding query, and requerying the DB.
The examples that I found online involved pagination of a DataTable that is rebuilt every time the page loads (e.g., Not Page.IsPostBack).
Here is a rough outline of our code:
Public dtCustomers As DataTable = New DataTable()
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load
' When the page first loads, show the search form with search options.
If Not Page.IsPostBack Then
ShowSearchForm()
' Once the form is submitted, show the search results.
Else
' ----------------------------------------
' This is the part that I'm having trouble with. How do I skip the following two steps (GatherFormData() and BuildDT()) when the user requests a subsequent page?
GatherFormData()
dtCustomers = BuildDT()
' ----------------------------------------
BindData()
End If
End Sub
Sub BindData()
Dim objPDS As PagedDataSource = New PagedDataSource()
objPDS.DataSource = dtCustomers.DefaultView
objPDS.AllowPaging = True
objPDS.PageSize = intNumPerPage
objPDS.CurrentPageIndex = Me.ViewState("_CurrentPage")
rptSearchResults.DataSource = objPDS
End Sub
' Subroutine called when the previous page button is pressed.
Sub GoToPrevPage()
' Set viewstate variable to the previous page.
Me.ViewState("_CurrentPage") -= 1
BindData()
End Sub
' Subroutine called when the next page button is pressed.
Sub GoToNextPage()
' Set viewstate variable to the next page.
Me.ViewState("_CurrentPage") += 1
BindData()
End Sub
Note: I understand that the DataTable will have to be put into cache or a session variable, but haven't decided the best method.
Please excuse the code outline but the actual code is massive so the simplification is to make it easier to get to the heart of the issue.
Let me know if any of that was unclear. Thanks in advance!
I am assuming that you are going to store your data in session/cache (I would prefer the later but there can be use case for storing it in session) - you can store the key in the view-state and presence of key could be use to determine if post-back is for pagination or not. For example,
if (ViewState["dataKey"] == null)
{
// first form submittal, do the search
GatherFormData();
dtCustomers = BuildDT();
// store it in cache
ViewState["dataKey"] = Guid.NewGuid().ToString(); // create a key
Cache.[ViewState["dataKey"].ToString()] = dtCustomers; // TODO use Add method to control expiration etc
}
Its important that you clear the view-state when the search is reset (or similar condition)
Lastly, it is also possible to do the paging from database/data-store (for example, using ranking functions in SQL Server) so that you need not have to store the search results into the session/cache but rather do a database trip at each post-back. Such approach is useful when the complete result set size can be huge.

Loop through Dynamically Created Controls

What I am trying to do is dynamically create a bunch of dropdown lists and then I want to loop through them and update the database with those values, but the problem I am facing is that I create the dropdown lists, but then when I go back to loop through them they are no longer in the panel. I don't know why but when I am debugging I have a count of 50 controls in pnlTeacherSelect right up until I press the button that calls prcChoose.
Called on Page Load
Sub prcSetTeachers()
For Each Subject In ds.Tables("Subject").Rows
Dim Temp As New DropDownList
pnlTeacherSelect.Controls.Add(Temp)
Temp.ID = "drp" & Subject.Item(0) & "s" & Child.Item(0)
Next
End Sub
Called on Clicking a button
Sub prcChoose()
For Each DropDownList In pnlTeacherSelect.Controls.OfType(Of DropDownList)
'This is never executed
Next
End Sub
Any ideas what's causing it? Thanks in advance!
You have to recreate all dynamically created controls on every postback(in load event at the latest).
You also have to ensure that they get the same ID as before to trigger events and maintain ViewState.
If you know the number of controls to create(which could be stored in ViewState) you can derive the ID from the counter variable by appending it to the control-id. Then you can recreate them with the correct ID in page's init event.
Recommandable readings:
TRULY Understanding Dynamic Controls
Page-Lifecycle
Or you use one of the builtin Data-Bound Control like Repeater that do this automatically. You only have to set their DataSource and call DataBind().
Here are answers of me on similar questions with implementations:
C#
VB.NET (+ C#)

Resources