How Grid paging works? - asp.net

The paging is working fine here, the problem is I don't know how it works, because when I put a break point and a logger in the GetCustomers method, I found that parameters maximumRows and startRowIndex are always 0, 0.
I have no clue why StartRowIndexParameterName, and MaximumRowsParameterName are created and how they are used. If they are used in custom mode, so how to enter that mode?
Thanks
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectCountMethod="Count"
SelectMethod="GetCustomers" TypeName="Pilots.BLL.Customer">
<SelectParameters>
<asp:Parameter Name="maximumRows" Type="Int32" />
<asp:Parameter Name="startRowIndex" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageIndex="0" PageSize="10"
DataSourceID="ObjectDataSource1">
</asp:GridView>
</div>
</form>
</body>

Here's a pretty thorough MSDN Article on GridView and Paging. Should be able to answer most all of your questions.

Those 2 parameters are used for custom paging on the gridview. That will be passed to SQL server to determine at what index to start the page (startRowIndex) and how many items on one gridview page (maximumRows).
The 'startRowIndex' will change depending on what the GridView.PageIndex is.
The number of rows returned from the method depends on what value set on 'maximumRows'.

It seems that I had to use EnablePaging as descried here,
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.enablepaging.aspx

Related

populating dropdownlist from another dropdown selection in ASP.Net Web Form

I am trying to Populate data into the Company DropDownList after selecting the Country from the CountryDropDownList. Complete newbiew on ASP.Net Web Form. So please excuse little knowledge.
I have been trying to do it only on the .aspx page which I am not entirely sure if I could do it. My code is as follows, which doesn't populate anything on the CompanyDropDownList at all. The problem is I have been writing the code looking at different sources but I guess I am not entirely sure where the problem is and what am I doing wrong. If anyone can explain a little bit and spot my errors would be really helpful.
<asp:DropDownList
ID="CompanyDropDownList"
runat="server"
OnSelectedIndexChanged="CompanyDropDownList_SelectedIndexChanged"
DataSourceID="CompanyNameSqlDataSource"
DataTextField="CompanyName"
DataValueField="CompanyID"
AppendDataBoundItems="True"
AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource
ID="CompanyNameSqlDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:BIGroupCostConnectionString %>"
OnSelecting="CountryNameSqlDataSource_Selecting"
SelectCommand="SELECT CompanyName, countryID FROM Company Where CompanyID = #CompanyID">
<SelectParameters>
<asp:ControlParameter
Name="CountryID"
ControlID="CountryDropDownList"
PropertyName="SelectedValue"
Type= "String"/>
<asp:Parameter Name="CountryID" />
</SelectParameters>
</asp:SqlDataSource>
Thanks.
I think the problem is that you are passing the company drop-down as parameter instead of the country you mention.
<SelectParameters>
<asp:ControlParameter Name="CompanyID" ControlID="CompanyDropDownList"
PropertyName="SelectedValue" Type= "String"/>
<asp:Parameter Name="CompanyID" />
Shouldn't it be your Country control?
Ademar
Found the problem. The problem was the following line of code. Removing the line showed the data correctly.
<asp:Parameter Name="CountryID" />

Using a dropdownlist control inside a formview

I am new to asp.net and i have a problem using a dropdownlist control inside a formview and passing its value to the related sqldatasource. When i use the code below i get the following exception
Exception Details: System.InvalidOperationException: Could not find control 'ddlCategory' in ControlParameter 'categoryId'.
The Dropdownlist inside a formview.
<asp:DropDownList ID="ddlCategory" DataSourceID="ObjectDataSourceCategory" DataTextField="NAME" DataValueField="ID" runat="server" />
The SQL Data Source
<asp:ObjectDataSource ID="sqlDataSourceItem" TypeName="Item" runat="server"
SelectMethod="getItem"
InsertMethod="insertItem"
UpdateMethod="updateItem">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="id" Name="id" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter ControlID="ddlCategory" Name="categoryId" PropertyName="selectedValue" />
</InsertParameters>
</asp:ObjectDataSource>
And i have found the solution to this problem. I have changed the ID of the DDL in the control parameter. It works like below since this is the final generated id of that control. But i think there must be an easier and better way. Any help would be appriciated.
<asp:ControlParameter ControlID="ctl00$main$frmViewItem$ddlCategory" Name="categoryId" PropertyName="selectedValue" />
This answer will provide a solution to your problem:
You need a recursive findcontrol() method.
It is because of your ddlCategory is inside formview and you are using the master page. The best way is to overwrite the 'FindControl' function of master page.
pls see the following link for detail:
http://geekswithblogs.net/AzamSharp/archive/2006/08/27/89475.aspx

How do I bind dependent drop down lists in ASP.NET

I need a way to bind multiple drop down lists that are populated based on their parent node.
RootDDL <- child <- grandchild[]
The code looks something similar to
<FormView DataSoureceID="rootDatasource">
<DropDownList ID="RootDLL" AutoPostBack="true">
<!--items-->
</DropDownList>
<DropDownList ID="child" AutoPostBack="true" DataSourceID="ChildDataSource" />
<SqlDataSource ID="ChildDataSource" />
<DropDownList ID="grandchild" DataSourceID="GrandChildDataSource" SelectedValue='<%# Bind('SomeFieldInRootDatasource') %>' />
<SqlDataSource ID="GrandChildDataSource">
<SelectParameters>
<ControlParameter ControlID="child" PropertyName="SelectedValue" />
</SelectParameters>
</SqlDataSource>
<Button Command="Update" />
</FormView>
<SqlDataSource ID="rootDatasource">
<InsertParameters>
<asp:Parameter Name="SomeFieldInRootDatasource" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="SomeFieldInRootDatasource" />
</UpdateParamters>
</SqlDataSource>
Changing the rootDDL works, unfortunately when the Child selectedvalue changes the grand child <%# Bind %> tries to rebind but cannot as it the FormView is no longer acting as a container.
It fails with
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Is there any solution to this problem that will not require me to add event (multiple) handlers?
I ended up creating a hiddenfield for each child dropdownlist that I then bound with the value I needed. I created a generic indexchange handler to then update the hiddenfeild with the desired result. This results in the bound field not being updated by a datasource (avoiding the rebind system call that was causing the issue in the first place).
Not sure if this is the best solution but it should work for most cases.

GridView and DropDownlist

I have a gridview that when enter to edit mode one of the column change to dropdownlist:
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name">
</asp:DropDownList>
</EditItemTemplate>
Now i have a SqlDataSource with update method that define in this aspx file:
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:ControlParameter ControlID="DropDownList2" Type="string"
PropertyName="SelectedValue" Name="genre" />
</UpdateParameters>
now i want to get the selected value and insert it but when i press the Update button in the row in the gridview i get this error:
Could not find control 'DropDownList2' in ControlParameter 'genre'
any idea why it happen?
Yes. ControlParameters only work when the DOM can find the control you're referring to that's within the same branch of the GridView. The problem is that Gridviews are a poor way of handling DOM controls because as soon as you go into a "mode" of the GridView like the EDIT mode, the entire DOM structure changes. The DOM by the way, is the Document Object Model. Because it's changed, therefore ASP cannot find the control you're referring to.
I've overcome this by doing one of two things.
First see if it works by simply tailing the control name with the '$' character.
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:ControlParameter ControlID="MyGridView$DropDownList2" Type="string"
PropertyName="SelectedValue" Name="genre" />
</UpdateParameters>
This sometimes works if you're lucky.
If not, then you'll need to find the control, get its value and pass it into the SQL parameter programmatically in code behind.
Try something like this (I use C#) ...
protected void MyGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
...
GridViewRow gvRow = (GridViewRow)sender;
DropDownList myDDL = (DropDownList)gvRow.FindControl("DropDownList2");
if (myDDL != null)
{
//assuming your datasource is outside the gridview, you have to find it!
SqlDataSource sds1 = (SqlDataSource)page.FindControl("myDataSource");
if (sds1 != null)
{
sds1.UpdateParameter["genre"].DefaultValue = myDDL.SelectedValue;
... and do your databinding etc
sds1.Update();
}
}
}
I always rely on the client-side code (ASPX) to do most of the work for me, like Bind(), Eval(), etc, but over time you'll realise that to really control your program, you'll have to rely on JavaScript or code behind to do the finer bits. ASP is not always "clever" in doing what you expect it to do.

How do you have multiple controls for a gridview?

For example, I have a gridview and two textboxes.
One textbox is for the text to search for.
The second textbox is an order number to search for.
I want my gridview to populate based on one or the other. I don't know how to tell my form if the user is using a number search by that and if a name, instead search by that.
Thanks for any help.
OK hope you haven't solved this yet because I took a few minutes to come up with an example that I think will do pretty much what you want.
DB access uses a stored procedure but you can use a ObjectDataSource with DAL, or just inline the SQL statement on the SqlDataSource, etc.
Markup:
Product ID:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="You must enter a number"
ValidationGroup="vg1" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
<br />
Description:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="cmdSearch" runat="server" Text="Search" ValidationGroup="vg1" /><br />
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="spGetProducts"
CancelSelectOnNullParameter="False" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" PropertyName="Text" DbType="String" DefaultValue="" />
<asp:ControlParameter ControlID="TextBox2" PropertyName="Text" DbType="Int32" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
And T-SQL for your query:
CREATE PROCEDURE spGetProducts
#ProductId int = NULL
,#ProductDescription nvarchar(100) = NULL
AS
BEGIN
SELECT [ProductId]
,[ProductDescription]
FROM [Products]
WHERE (
(
(#ProductId IS NULL)
OR
([ProductId] LIKE % + #ProductId + %)
)
AND
(
(#ProductDescription IS NULL)
OR
([ProductDescription] LIKE % + #ProductDescription + %;)
)
);
END
If the user doesn't enter anything in either of the fields, the SqlDataSource will still bind due to SqlDataSource.CancelSelectOnNullParameter = False but the empty parameter will not be sent with the query due to ControlParameter.DefaultValue being set. The stored procedure will then insert the NULL value into the parameter and basically skip that part of the filtering in the WHERE clause.
Hope this helps.
You can check the textboxes by using (TextBox1.Text.Trim.Length > 0) or (TextBox1.Text = "")
It sounds like what you are really asking is how do you filter your data source based on more than one possible filter parameter. Explaining that would require knowing what your data source is. Either way, the gridview is just going to display the filtered results, right?
If you are using SQL for your data source the technique is going to be totally different than filtering a collection in memory. So more information on that would be helpful.

Resources