I am using AJAX TabContainer and have an asp GridView in each tab which is bound to an ObjectDataSource. The ObjectDataSource returns different objects based on the tab selected (TabContainer AutoPostBack="True"). I bind the ObjectDataSource to the GridView based on the tab selected during TabContainer Load in the code behind only if IsPostBack because on the first load the TabContainer is not visible. I do not bind the GridView anywhere else. The fields in the GridView are Eval instead of Bind because the parameters are added to the ObjectDataSource dinamically.
This is a bulk update GridView with all of the fields editable. When I update the GridView, I am not able to see e.OldValues. If the fields in the GridView are set to Bind, I am able to retrieve e.NewValues nut e.OldValues is still empty...
Does anyone know what the deal is?
<asp:ObjectDataSource ID="odsEquipment" runat="server" TypeName="EquipmentDB" SelectMethod="GetEquipment" SortParameterName="sortExpression" UpdateMethod="UpdateEquipment">
<SelectParameters>
--Params
</SelectParameters>
<UpdateParameters>
--Params
</UpdateParameters>
</asp:ObjectDataSource>
<asp:TabContainer ID="tcDisciplines" runat="server" CssClass="ajax_tabController" ScrollBars="Horizontal" AutoPostBack="True">
--Tabs and GridViews with no properties
Code Behind:
Protected Sub tcDisciplines_Load(sender As Object, e As EventArgs) Handles tcDisciplines.Load
If IsPostBack Then
For Each discipline In disciplineList
tcDisciplines.Tabs(discipline.ID).HeaderText = discipline.Discipline
tcDisciplines.Tabs(discipline.ID).TabIndex = discipline.ID
Dim gv As GridView = TryCast(tcDisciplines.Tabs(discipline.ID).Controls(0).FindControl("gv" + discipline.Abbr), GridView)
gv.Visible = False
If tcDisciplines.ActiveTabIndex = discipline.ID Then
gv.Visible = True
'Set properties
End If
Next
End If
End Sub
Found a little tutorial on MSDN, http://msdn.microsoft.com/en-us/library/aa992036.aspx#Y3473
This work-around helped me. I used a session instead with a list of rows.
<asp:ControlParameter ControlID="ddListPlayerPointSystems" Name="profileid" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ddListCmty" Name="cmty" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ctl00$MainContent$TabContainer1$TabPanel1$FormView3$pointsTextBox" Name="InsertPts" PropertyName="Text" Type="Decimal" />
I am having trouble understanding why in the first controlparameter i can call the dropdownbox id but not the textboxes id which is pointsTextBox. I am using a master page with an asp ajax tab container with multiple panels. If i take off the "ctl00$MainContent$TabContainer1$TabPanel1$FormView3$" i get a control not found but i dont know why this works for the other two controlparameters
EDIT
So I found a solution to my problem. Thanks to #TheGeekYouNeed and #JamesJ I understand why I would require the longer path name for that particular textbox (the drops were outside of the tabcontainer so the direct name worked). But I found that since I was assigning the value of that textbox via '<%# Bind("name", "{0:n}") %>' I was able to instead just use an asp:Parameter rather than the ControlParameter like so:
"<asp:Parameter Name="name" Type="String" />"
Problem is that i don't quite understand how that all works.
The ControlID for the pointsTextBox is not 'ct100$MainContent$TabContainer..etc... on the server side.
Set the COntrolID in the code behind, so you can use FindControl("pointsTextBox") to get a reference to the textbox control.
You could do something like:
TextBox t = this.FindControl("pointsTextBox") as TextBox;
if(t != null)
{
ddListPlayerPOintSystems.Add(new { COntrolID = t, Name = "InsertPts", PropertyName="Text", Type="Decimal"});
}
I haven't tested it, so I am not claiming the code is perfect, but the method you need to follow is illustrated here.
I have two cascading dropdown lists I'm attempting to bind to two separate SqlDataSources each.
These dropdownlists exist in a FormView's EditItemTemplate. Inside the EditItemTemplate two sqldatasource controls exist that populate the department and the jobname. The DeptID and the JobID are the primary keys in those tables. This creates the "cascading effect" between departments and jobs. When a department is selected, only the jobs associated with that department appear.
This piece is working properly.
<asp:FormView ID="frmProfile" runat="server" DataSourceID="sqlDSProfile"
DataKeyNames="EUID" style="margin-top: 0px">
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartments" runat="server" Width="135px"
DataSourceID="sqlDSDepartments"
DataTextField="Department"
DataValueField="DeptID" AutoPostBack="True"
SelectedValue='<%# Bind("CurrentDeptID") %>'
AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlJobNames" runat="server" Width="185px"
DataSourceID="sqlDSJobs" DataTextField="JobName" DataValueField="JobID"
SelectedValue='<%# Bind("CurrentJobID") %>'
AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sqlDSDepartments" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT tblDepartments.DeptID,
tblDepartments.Department
FROM tblDepartments" />
<asp:SqlDataSource ID="sqlDSJobs" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT tblJobs.JobID, tblJobs.JobName FROM tblJobs
INNER JOIN tblDeptsJobs ON tblDeptsJobs.JobID = tblJobs.JobID
WHERE tblDeptsJobs.DeptID = #DeptID" >
<SelectParameters>
<asp:ControlParameter ControlID="ddlDepartments" Name="DeptID"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
</asp:FormView>
Outside the formview the SqlDataSource exists that binds all of the information to the Employee table in an update statement. I'm leaving all of the other information in this SqlDataSource even though it's been omitted from the FormView above.
<asp:SqlDataSource ID="sqlDSProfile" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT tblEmployee.EUID,
tblEmployee.DateHired,
tblEmployee.LastName,
tblEmployee.HiredLastName,
tblEmployee.FirstName,
tblEmployee.Role,
tblEmployee.JobGrade,
tblEmployee.CurrentDeptID,
tblDepartments.Department,
tblDepartments.DeptID,
tblEmployee.CurrentJobID,
tblJobs.JobName,
tblJobs.JobID,
tblEmployee.CurrentShift,
tblEmployee.JobDate,
tblEmployee.IsDisplaced,
tblEmployee.EligibilityDate
FROM tblEmployee
LEFT OUTER JOIN tblDepartments ON tblEmployee.CurrentDeptID = tblDepartments.DeptID
EFT OUTER JOIN tblJobs ON tblEmployee.CurrentJobID = tblJobs.JobID
WHERE (tblEmployee.EUID = #EUID)"
UpdateCommand="UPDATE [tblEmployee]
SET [tblEmployee].[DateHired] = #DateHired,
[tblEmployee].[LastName] = #LastName,
[tblEmployee].[HiredLastName] = #HiredLastName,
[tblEmployee].[FirstName] = #FirstName,
[tblEmployee].[Role] = #Role,
[tblEmployee].[JobGrade] = #JobGrade,
[tblEmployee].[CurrentDeptID] = #CurrentDeptID,
[tblEmployee].[CurrentJobID] = #CurrentJobID,
[tblEmployee].[CurrentShift] = #CurrentShift,
[tblEmployee].[JobDate] = #JobDate,
[tblEmployee].[IsDisplaced] = #IsDisplaced,
[tblEmployee].[EligibilityDate] = #EligibilityDate
WHERE [tblEmployee].[EUID] = #EUID"
ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:SessionParameter Name="EUID" SessionField="sProfileEUID" DbType="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DateHired" DbType="Date" />
<asp:Parameter Name="LastName" DbType="String" />
<asp:Parameter Name="HiredLastName" DbType="String" />
<asp:Parameter Name="FirstName" DbType="String" />
<asp:Parameter Name="Role" DbType="String" />
<asp:Parameter Name="JobGrade" DbType="Byte" />
<asp:Parameter Name="CurrentDeptID" DbType="Int32" />
<asp:Parameter Name="CurrentJobID" DbType="Int32" />
<asp:Parameter Name="CurrentShift" DbType="Int32" />
<asp:Parameter Name="JobDate" DbType="Date" />
<asp:Parameter Name="IsDisplaced" DbType="Boolean"/>
<asp:Parameter Name="EligibilityDate" DbType="Date"/>
<asp:SessionParameter Name="EUID" SessionField="sProfileEUID" DbType="String" />
</UpdateParameters>
</asp:SqlDataSource>
The only pieces I can't figure out how to bind are the Departments and the Jobs. Everything else is working. I've tried using the following code in the DropDownList controls...
SelectedValue='<%# Bind("CurrentDeptID") %>'
SelectedValue='<%# Bind("CurrentJobID") %>'
...but these result in errors.
Summary
When the user clicks edit, I need the values in the two dropdownboxes to pull their selectedvalue from the main sqlDSProfile data source, but I need them to be updatable. I've gotten it to the point where I can update and bind the job that an associate belongs to, but because the dropdownlists cascade, when I attempt to change the department the AutoPostBack breaks the binding between sqlDSProfile - CurrentJobID and ddlJobs.
Update
I added tblEmployee.CurrentDeptID and tblEmployee.CurrentJobID to the select statement, and added Bind() statements to the DropDownList controls.
SelectedValue='<%# Bind("CurrentDeptID") %>'
SelectedValue='<%# Bind("CurrentJobID") %>'
The two DropDownLists are now populated with accurate information pulled from the Employee table, showing the department and job that the employee belongs to.
The two DropDownLists are also populated by the two SqlDataSources inside the FormView, giving me options for changing the department and changing the job.
When I change the Job, it works and the employees job is updated.
When I change the Department, it breaks saying DataBinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Close to Done
I removed the data binding from ddlJobs and coded that in the background.
Protected Sub frmProfile_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles frmProfile.ItemUpdating
If frmProfile.CurrentMode = FormViewMode.Edit Then
e.NewValues("CurrentJobID") = DirectCast(DirectCast(sender, FormView).FindControl("ddlJobs"), DropDownList).SelectedValue
End If
End Sub
The only piece that's left is building the code for when the ddlDepartments changes.
pseudocode...
' If Item exists in ddlJobs Then
' select item (CurrentJobID)
' else
' select index 0 and make them pick something new
' end if
So Close!
Updated Again
This is the code I've developed to loosely bind this. In the page_load I'm trying to pull the contents of CurrentJobID from sqlDSProfile and check to see if that value exists in ddlJobs. If it does I want to set ddlJobs.SelectedValue = to that CurrentJobID. If it doesn't I want to set the selectedindex to 0 which is a message saying "pick one" or something.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If frmProfile.CurrentMode = FormViewMode.Edit Then
' Need to determine if the CurrentJobID returned in the select statement
' exists in the ddlJobs dropdownlist. If it does, set that to the
' selectedvalue, if not set it to 0 so the user can select a new job.
Dim ddlJobs As DropDownList = frmProfile.FindControl("ddlJobs")
Dim dvProfile As DataView = sqlDSProfile.Select(DataSourceSelectArguments.Empty)
Dim drvProfile As DataRowView = dvProfile(0)
If ddlJobs.Items.FindByValue(drvProfile("CurrentJobID")) Is DBNull.Value Then
ddlJobs.SelectedIndex = 0
Else
ddlJobs.SelectedValue = drvProfile("CurrentJobID")
End If
End If
End Sub
Its returning a null reference exception on the line where I'm checking for dbnull.value
I had a similar issue and found a very simple resolution, (and in c#). Imagine a database with a table Questions, related to category and subcategory tables (which are also related and constrained). When trying to update existing records asp throws an error. This is the solution that I worked out thanks to the information above from Lucretius et al.
Only databind the parent dropdownlist
Find a way to insert the child dropdown selected value on the update event of the datasource.
As:
protected void odseditquestion_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
//dynamically assign value from ddlsubcategory to odseditquestion on updating event
//you really should not have to do this
DropDownList ddlsubcategory = (DropDownList)fveditquestion.FindControl("ddlsubcategory");
e.InputParameters["subcatid"] = (ddlsubcategory.SelectedValue);
}
It works on my application. Hope it helps somebody, this one cost me half a day, such is asp!!
The problem can be if tlbEmployee columns in the SqlDSProfile in the update statement and field names used by your controls do not match. Other procedures you've followed are right.
SqlDataSource control expects field names it updates to be similar
with those bound to the controls(fields) inside the DataBound control.
The Solution can be: change all the update Parameters to ControlParameters referencing the right control for each one
Update: Wait, I think the problem is your select statement of the SqlDSProfile should contain: CurrentDeptID and CurrentJobID. Try it:
<asp:SqlDataSource ID="sqlDSProfile" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT tblEmployee.EUID,
tblEmployee.DateHired,
tblEmployee.LastName,
tblEmployee.HiredLastName,
tblEmployee.FirstName,
tblEmployee.Role,
tblEmployee.JobGrade,
tblDepartments.Department,
tblJobs.JobName,
tblEmployee.CurrentShift,
tblEmployee.JobDate,
tblEmployee.IsDisplaced,
tblEmployee.EligibilityDate
tblEmployee.CurrentDeptID,
tblEmployee.CurrentJobID
FROM tblEmployee
Advice: Test your code portion by portion.
Try the code without the dropdownlist, Test separately
Add the one drop downlist
Use select * from ... in select queries
Avoid ajax when testing
If you make it
Add portions of code portion after portion
at last use the partial updating (ajax)
I have a working solution now, thanks in part to Nuux and a bunch of online research. The tip about the join statement wasn't relevant, but the tip about including "CurrentJobID" and "CurrentDeptID" in my select query was spot on.
In addition to that I had to rework the controls a little. The two cascading dropdownlists are below. The ddlJobs dropdown list behaves like a normal databound control, but it doesn't have the Bind("CurrentJobID") statement I was trying in my original post.
<asp:DropDownList ID="ddlDepartments" runat="server" Width="185px"
DataSourceID="sqlDSDepartments"
DataTextField="Department"
DataValueField="DeptID"
SelectedValue='<%# Bind("CurrentDeptID") %>'
AppendDataBoundItems="true"
AutoPostBack="True" >
<asp:ListItem Text="--Select One--" Value="" />
</asp:DropDownList>
<asp:DropDownList ID="ddlJobs" runat="server" Width="185px"
DataSourceID="sqlDSJobs"
DataTextField="JobName"
DataValueField="JobID"
AppendDataBoundItems="true"
OnDataBinding="ddlJobs_DataBinding" />
The only thing the custom routine "ddlJobs_DataBinding" is doing is adding "--Select One--" as index 0 in the ddlJobs dropdown. I tried this in several places, like page_load, and the databound event of the formview with no success.
Protected Sub ddlJobs_DataBinding(sender As Object, e As System.EventArgs)
Dim ddlJobs As DropDownList = frmProfile.FindControl("ddlJobs")
Dim liSelectOne As New ListItem("--Select One--", 0)
ddlJobs.Items.Clear()
ddlJobs.Items.Insert(0, liSelectOne)
End Sub
The databound event of the formview frmProfile_DataBound event does do some work though. When the user clicks "edit" on the formview to enter editing mode this ensures that the dropdownlist ddlJobs has the correct job selected by default for the profile in question. If the user hasn't been assigned to a job then it defaults to selectedindex 0 which is "--Select One--" set in custom databinding event just above.
Protected Sub frmProfile_DataBound(sender As Object, e As System.EventArgs) Handles frmProfile.DataBound
If frmProfile.CurrentMode = FormViewMode.Edit Then
Dim ddlJobs As DropDownList = frmProfile.FindControl("ddlJobs")
Dim dvProfile As DataView = sqlDSProfile.Select(DataSourceSelectArguments.Empty)
Dim drProfile As DataRow = dvProfile.Table.Rows(0)
If drProfile("CurrentJobID").ToString() = "" Then
ddlJobs.SelectedIndex = 0
Else
ddlJobs.SelectedValue = drProfile("CurrentJobID").ToString()
End If
End If
End Sub
Finally, if the user selects a new job from ddlJobs, that value has to be fed to the database, which the ItemUpdating event of the formview handles.
Protected Sub frmProfile_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles frmProfile.ItemUpdating
If frmProfile.CurrentMode = FormViewMode.Edit Then
Dim ddlJobs As DropDownList = frmProfile.FindControl("ddlJobs")
e.NewValues("CurrentJobID") = ddlJobs.SelectedValue
End If
End Sub
Done!
How can I change my selecommand, and keep it through the remainder of the page (when using pagination, sorting)?
I have a page of checkboxes:
<input type="checkbox" name="checkbox_1" />
<input type="checkbox" name="checkbox_2" />
<input type="checkbox" name="checkbox_3" />
<asp:Button runat="server" Id="CustomButton" text="Create Report" PostBackUrl="report.aspx?"/>
Then on report.aspx I want to generate a standard listview based on the selections in the checkbox.
<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
...<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />...
</LayoutTemplate>
<ItemTemplate>
...
</ItemTemplate>
</asp:ListView>
I want to be able to sort and paginate that listview. This is an idea of what i want in the code behind:
Protected Sub ReportListView_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
' What's the correct way to reference the listview?
' When I use the below code i get "ReportListView is not declared...."
' ReportListView.SqlCommand = "SELECT " & checkbox1 & ", " & checkbox2 & " WHERE..."
End Sub
I'm not sure if I'm even going in the right direction with this, any help is appreciated. Will the changes i make to the sql command in the PreRender function hold when I have applied pagination or sorting to the listview?
If I understand your question correctly, you want to open a new page and use the prior page's values in the select statement for the ListView's SqlDataSource on the new page, correct?
First, a few observations:
In your first page, you appear to be intending to call the second page with a query string (PostBackUrl="report.aspx?), but you don't appear to set the query string.
Your PreRender event for the ListView control has the wrong signature. It only takes one argument, EventArgs:
Protected Sub ReportListView_PreRender(ByVal e As EventArgs)
Your ListView appears to be using a SqlDataSource as it's binding source (DataSource="ReportListViewSDS"). More about that below.
There is no SqlCommand property or method for the ListView control.
Since you're binding the ListView to a SqlDataSource, it'd be simplest to set the Select command and the parameters in the markup, like this:
<asp:SqlDataSource ID="ReportListViewSDS" runat="server"
SelectCommand="SELECT checkbox1, checkbox2, checkbox3 FROM <table> WHERE checkbox1 = #parm1 AND checkbox2 = #parm2 AND checkbox3 = #parm3">
<SelectParameters>
<asp:FormParameter FormField="checkbox_1" Name="parm1" />
<asp:FormParameter FormField="checkbox_2" Name="parm2" />
<asp:FormParameter FormField="checkbox_3" Name="parm3" />
</SelectParameters>
</asp:SqlDataSource>
Replace <table> in the SelectCommand with the name of your table. You can adjust the names of the columns you're selecting, as well as the parameters you're using, as desired. I simply used 3 checkboxes as that's what you had in the code you posted.
Also note, NO VALIDATION of the parameters will be done by the SqlDataSource, so if you want to prevent SQL Injection attacks and other security risks, you'll want to do validation in the Selecting event of the SqlDataSource.
More information can be found here:
SqlDataSource Class
FormParameter Class
Actually this was much easier than I thought. Sorry, just a newbie mistake i guess. i ended up simply doing:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SqlCommand As String
' create the sql command using the Request.Form vars i wanted.
' ...
' Run the sql command, I can access the listview directly, just like a global variable:
ReportListView.SelectCommand = SqlCommand
ReportListView.DataBind()
End Sub
And that seemed to do it. Actually very easy.
I want to set a parameter for a SqlDataSource programmatically as described in Step 5 at http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-vb . Also a GridView is bound to the sqlDataSource. My markup is:
<asp:SqlDataSource ID="mySqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringHTL %>"
SelectCommand="SELECT [subscription_con] FROM [HTL_CONSUME_CON] WHERE ([subscription_con] = #subscription_con)">
<SelectParameters>
<asp:Parameter Name="subscription_con" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="myGridView" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="mySqlDataSource">
</asp:GridView>
In the codebehind, I have:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mySqlDataSource.SelectParameters("subscription_con").DefaultValue = calcResult()
End Sub
The return value from calcResult() is different for each postback. The postback occurs when the user clicks a Button on the form that has UseSubmitBehavior=True.
I've use the debugger to step through the code behind and I see it executed for each page load and I see the expected values returned from clacResult().
However the bound DataGrid is never updated on the postbacks, it only updates on the first page load.
If I change the SqlDataSource parameter to have a control as the source, then it works on postbacks. In otherwords, I changed the markup to use:
<asp:ControlParameter ControlID="myTextBox" Name="subscription_con" PropertyName="Text" Type="Int32" />
and I changed the code behind to be:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
myTextBox.Text = calcResult()
End Sub
With the TextBox control as the source of the SqlDataSource parameter, update of the GridView works for the original page load and all postbacks. However, I really have no need for the TextBox and would prefer not to use it.
What am I missing about how to set a parameter programmatically for an SqlDataSource? Why doesn't the bound GridView get updated on postbacks when setting the SqlDataSource parameter programmatically when there is no control source?
You may need an explict databaind on postbacks:
if (Page.IsPostBack)
{
grid.DataBind();
}
Should do it.
C#
SqlDataSource1.SelectParameters["Where_Clause"] = new Parameter() { Name = "Where_Clause", DefaultValue = "WHERE m.Id = 1" };
Greedy Networks.