QueryExtender with dropdown list - asp.net

i am using QueryExtender with dropdownlist to filter gridview ( datasource : EntityDataSource).
<asp:SearchExpression SearchType="StartsWith" DataFields="Status" >
<asp:ControlParameter ControlID="ddlStatus" Type="String" />
</asp:SearchExpression>
Where i bind my ddlStatus from database with default value : "Select"
But when i run project it takes by default value "Select" for Field "status" and gives empty grid.
But on Pageload i want to show all the records after user can select different status from dropdownlist and based on that filter should work.
how can we show all the data with dropdownlist value selected as default "select"

Just Found solution here in book: Entity Framework 4.0 Recipes: A Problem-Solution Approach
Used PropertyExpression instead of SearchExpression
<asp:PropertyExpression>
<asp:ControlParameter ControlID="ddlStatus" Type="String" />
</asp:PropertyExpression>
and leave value blank according to Bala R comment
<asp:ListItem Text="Select" Value="" />

Try using DefaultValue like this
<asp:ListItem Text="Select" Value="Select" />
and
<asp:SearchExpression SearchType="StartsWith" DataFields="Status" >
<asp:ControlParameter ControlID="ddlStatus" Type="String" Default="Select" />
</asp:SearchExpression>

Related

how to use url parameters in commandparameters

i'm working on asp project and i want to use commandparameters
my question is how to put the value of controlid by using url parameter
i try this but doesnt working
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=DentistEntities" DefaultContainerName="DentistEntities" CommandText="SELECT p.[id],p.[firstName],p.[lastName],p.[DOB],p.[firstVisiting] FROM DentistEntities.Patients AS p WHERE p.[firstName]=#firstname">
<CommandParameters>
<asp:ControlParameter Name="firstname" ControlID='<% Request.QueryString["firstName"] %>' Type="String"/>
</CommandParameters>
</asp:EntityDataSource>
error on page
Could not find control '<% Request.QueryString["firstName"] %>' in ControlParameter 'firstname'.
There is a special QueryStringParameter for that:
<asp:QueryStringParameter Name="firstname" QueryStringField="firstName" Type="String"/>

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 to pass value using ImageButton to update database?

I having problem when I try to pass value from ImageButton to a controlparameter, then the update command can retrieve the value from control parameter to execute update statement.
I want to pass value Status=1 when ImageButton APPROVE is clicked, else pass value Status=2 when ImageButton REJECT is clicked.
Where and how should I assign value Status?
When I run my code, I receive this error : Must declare the scalar variable "#Status".
Or any recommendation to pass the value Status?
My ImageButton:
<ItemTemplate>
<asp:ImageButton runat="server" ID="APPROVE" CommandName="update"
ImageUrl="~/images/accept.png"
OnClientClick="if (!window.confirm('Are you sure you want to approve this booking?')) return false;" />
</ItemTemplate>
<ItemTemplate>
<asp:ImageButton runat="server" ID="REJECT" CommandName="update"
ImageUrl="~/images/reject.png"
OnClientClick="if (!window.confirm('Are you sure you want to reject this booking?')) return false;" />
</ItemTemplate>
My UPDATE statement
UpdateCommand="UPDATE [bookingschedule] SET status=#Status WHERE [bookingScheduleID] = #bookingScheduleID"
My ControlParameter
<UpdateParameters>
<asp:Parameter Name="bookingScheduleID" Type="Int32" />
<asp:ControlParameter Name="Status" ControlID="APPROVE" Type="Int32" />
<asp:ControlParameter Name="Status" ControlID="REJECT" Type="Int32" />
</UpdateParameters>
A button does not have a value in html and thus you can't use it in the ControlParameter declaration. One solution is to make a hidden textbox on the form. Lets call it "StatusType".
<input type="hidden" name="StatusType" value="0">
Now, in the "OnClientClick" of each of the buttons; assign the StatusType's value to either 1 or 2 (or whatever your defined status codes are).
OnClientClick="if (!window.confirm('Are you sure you want to reject this booking?')) {
return;
}
else {
$('#StatusType').val(0);
}"
Then use "StatusType" in the ControlParameter.
<asp:ControlParameter Name="Status" ControlID="StatusType" Type="Int32" />
Have you tried using the CommandArgument property of the ImageButton?
From MSDN:
Sometimes, multiple ImageButton controls are related and share the same value for the CommandName property, such as Sort. Use this property to supplement the CommandName property with additional information about the command to perform, such as Ascending. The values of the CommandName and CommandArgument properties are typically used in the OnCommand event handler to determine the action to perform when the ImageButton control is clicked.
<asp:ImageButton id="imagebutton1" runat="server"
AlternateText="Sort Ascending"
ImageUrl="images/pict.jpg"
OnCommand="ImageButton_Command"
CommandName="Sort"
CommandArgument="Ascending"/>

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.

Filter expression not working properly

I am using a GridView and I require the user to be able to filter using 2 controls. One simply filters the type of row - there is a column called action, and the user selects one of the distinct values from the database in a dropdown box, and the gridview only displays the rows with that value in the action column. On it's own this works perfect.
But I am also adding a textbox where the user can type in an ID of either the 'itemID', or the 'parentID'. This works fine even with the previous control.
Problem is, the dropdown box does not work when the textbox is empty (i.e. all ID's are being shown). If I choose a value and click Submit, it just doesn't do anything. Now if I am filtering by ID and then choose a value from the dropdown box, it works perfect.
Here is my filter expression and parameters of the datasource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="cleared for privacy"
SelectCommand="SELECT * FROM [Audit] WHERE ([source] = #source)" FilterExpression="action like '{0}%' and (itemID like '{1}' or parentID like '{1}')">
<SelectParameters>
<asp:Parameter DefaultValue="LOGISTICS" Name="source" Type="String" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="action" ControlID="DropDownList1" PropertyName="SelectedValue" />
<asp:ControlParameter Name="legorinvid" ControlID="txtFilter" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
In my case, replacing the SQL filter expression
LIKE '{1}%' by LIKE '{1}'
made the difference.
The former did not work, the latter did.
Have you tried setting the ConvertEmptyStringToNull="false" field on the control parameter?

Resources