asp.ControlParamter control id conflict - asp.net

<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.

Related

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.

controlparameter not found

Error: Could not find control 'mytextfield' in ControlParameter 'mycontrolparam'.
<asp:ControlParameter ControlID="mytextfield" Name="mycontrolparam" PropertyName="Text" Type="Int32" />
The error is because "mytextfield" is inside a panel control and I would need FindControl method to find it. But I want to find a quick solution if there is any, how to make mytextfield visible to the control parameter. The same code works on a different page but not on this one. Please keep in mind, I want to make minimum changes to the existing code.
Here is similar code I copied over from Microsoft site.
<asp:SqlDataSource id="Employees" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind%>"
SelectCommand="SELECT LastName FROM Employees WHERE Title = #Title">
<SelectParameters>
<asp:ControlParameter Name="Title"
ControlID="DropDownList1"
PropertyName="SelectedValue"/>
</SelectParameters>
</asp:sqldatasource>
Want to mention: 'mytextfield' is a readonly textfield. It does work OK on the other page though.
Please add control parameter inside your codebehind page when sqldatasource is selecting. You will have to cast control first and then add parameters value. I assumed textbox xyz control inside panel named mypanel.
Protected Sub Employees_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles Employees.Selecting
Dim xyz As TextBox = DirectCast(mypanel.findcontrol("yourcontrolname"), TextBox)
Dim mycontrolparam = New SqlParameter("#mycontrolparam",xyz.text)
e.Command.Parameters.Add(mycontrolparam)
End Sub
Myself: If a control is embedded inside a panel, you can only find it through FindControl method of that panel. It still actually did not work for me and I had to abandon it and use a a different method.
Use the following method otherwise
FindControl("MyControlID")

EntityDataSource replace * with % wildcard on queries

I have an application that uses EntityDataSource in many places.
In the EDS, I manually build the Where clause based on user input from TextBox'es.
I would like the user to be able to enter "*" (asterisks) instead of "%" when querying data.
Is there an easy as using Entity SQL or the EDS itself to do a search/replace? I know I could actually change the TextBox after the data is entered, but when the user sees his text was changed from an * to a % I don't think he will understand.
I have tried using the T-SQL Replace command and doing something like this:
<asp:EntityDataSource ID="EDSParts" runat="server"
ConnectionString="name=TTEntities" DefaultContainerName="TTEntities"
EnableFlattening="False" EntitySetName="Parts"
OrderBy="it.ID DESC"
Where ="(CASE
WHEN (#PartNumber IS NOT NULL) THEN
it.[Number] LIKE REPLACE(#PartNumber, "*", "%")
ELSE
it.[ID] IS NOT NULL
END)">
<WhereParameters>
<asp:ControlParameter Name="PartNumber" Type="String"
ControlID="txtPartNumberQuery" PropertyName="Text" />
</WhereParameters>
</asp:EntityDataSource>
But I get a "Server tag is not well formed" message. I can't find an equivalent "replace" function in the Entity SQL reference....
Any ideas?
You can handle page postback and modify content of txtPartNumberQuery. EntityDataSource can work only with % (because it builds ESQL query) so you have to change * to % in your codebehind before you execute databinding.
Sluama - Your suggestion fixed it! Such an obvious answer. The " was terminating the Where clause string. I could have sworn I tried that, but I guess not. Becuase, I just happened to come back to this question and saw your answer and it works!
<asp:EntityDataSource ID="EDSParts" runat="server"
ConnectionString="name=TTEntities" DefaultContainerName="TTEntities"
EnableFlattening="False" EntitySetName="Parts"
OrderBy="it.ID DESC"
Where ="(CASE
WHEN (#PartNumber IS NOT NULL) THEN
it.[Number] LIKE REPLACE(#PartNumber, '*', '%')
ELSE
it.[ID] IS NOT NULL
END)">
<WhereParameters>
<asp:ControlParameter Name="PartNumber" Type="String"
ControlID="txtPartNumberQuery" PropertyName="Text" />
</WhereParameters>
</asp:EntityDataSource>

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?

LinqDataSource and DateTime Format

I'm trying to create a LinqDataSource to bind to a DropDownList in an ASP.NET form. I only want to show the elements according to a date (which is one of the fields in the database).
Basically, the elements I want to show are the ones that will happen in the futures (i.e. after DateTime.Now).
I was trying the following markup :
<asp:DropDownList runat="server" ID="DropDownList1"
AppendDataBoundItems="True" DataSourceID="LinqDataSource1"
DataTextField="TextField" DataValueField="ValueField">
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataContext1" TableName="Table"
Where="DateField >= #DateField">
<WhereParameters>
<asp:Parameter DefaultValue="DateTime.Now" Name="DateField"
Type="DateTime" />
</WhereParameters>
</asp:LinqDataSource>
I'm getting a format exception saying that "The string was not recognized as a valid DateTime" when I try to run it. However, the dates in my database seem to be fine, because a DateTime.Parse works perfectly on them. The DateField is of type datetime in SQL.
What am I missing here?
Thanks!
The DefaultValue was what was wrong with the code as was suggested by the others.
However, setting the DefaultValue to
"<%# DateTime.Now %>"
like Andomar suggested (which would make the markup look something like this :
<WhereParameters>
<asp:Parameter DefaultValue="<%# DateTime.Now %>" Name="DateField" Type="DateTime" />
</WhereParameters>
will not work either because DataBinding expressions are only supported on objects that have a DataBinding Event, and neither Parameter or ControlParameter have one.
For a String, it's fairly easy to create a TextBox or Label and put the <%# %> expression in the value of that new field (more details here), but it was a bit more complicated with a DateTime value, as comparing an SQL DateTime with a .NET DateTime caused an exception.
It can be done quite easily in the Page_Load event by using
DataContext DataContext1 = new DataContext();
var c = from a in DataContext1.Field
where a.DateField >= DateTime.Now
select a;
DropDownList.DataSource = c;
DropDownList.DataBind();
I suspect it is failing on the DefaultValue.
Try:
DefaultValue="<%# DateTime.Now %>"

Resources