Pass a this.property to ObjectDataSource - asp.net

A user control contains a ListView, ObjectDataSource and a CustomerID property, is there a way to pass CustomerID to ObjectDataSource.
it seems ControlParameter does not fix the problem.
Regards

ControlParameter must be used for to get a parameter from a Control. To get a parameter from a property you have to get it from code behind:
<asp:ObjectDataSource OnSelecting="OdsOnSelecting" .... >
<SelectParameters>
<asp:Parameter Name="CustomerID" />
</SelectParameters>
</asp:objectDataSource>
protected void OdsOnSelecting(object sender, ObjectDataSourceMethodEventArgs e) {
e.InputParameters["CustomerID"] = CustemerID;
}

Respond to this event:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.objectcreating.aspx

Try creating an attribute/property for the usercontrol in question, THEN do what is mentioned in the two post before this one...

Related

Storing another control value in a field of DetailsView Insert Interface

I have an asp.net program.
Here I want to keep Insert Facility of the Details view. But in the interface I want to keep the 'UserName' attribute of the new Record as fixed and same as the UserName of the Logged in user. I also want to check it on Server side whether it corresponds to the logged in user's username.
How can I achieve this functionality.
I have made the BoundField ReadOnly. But I can't find anything else suitable for this functionality.
In your .aspx do the following:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" OnInserting="SqlDataSource1_Inserting">
<InsertParameters>
<asp:Parameter Type="String" Name="Username" />
//Place other parameters here
</InsertParameters>
</asp:SqlDataSource>
In your code behind:
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
SqlDataSource1.InsertParameters["Username"].DefaultValue = Context.User.Identity.Name;
}

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.

Passing Parameters for SqlDataSource from code behind

whats the diffrence btw this two SqlDataSource1?
<SelectParameters>
<asp:Parameter Name="user" />
</SelectParameters>
SqlDataSource1.SelectParameters("user").DefaultValue = "some value";
SqlDataSource1.SelectParameters("#param",user);
When i use
SqlDataSource1.SelectParameters("user").DefaultValue = "some value";
it works but
SqlDataSource1.SelectParameters("#param",user); doesn't:
In the selecting event of the SqlDataSource control pass the value to the to the parameter:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["#user"].Value="value";
}
I think you should use smth. like this:
SqlDataSource1.SelectParameters.Add("#param",user);
that has some overloads.

Error in parameter that i use in update command in sqlDataSource

I use below code :
.aspx
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ConnectionStrings:Con %>" runat="server"
SelectCommand="select * from DorePayvar" OnSelecting="selecting"
UpdateCommand="update DorePayvar set Name=#name" OnUpdating="SqlDataSource1_Updating">
<UpdateParameters>
<asp:Parameter Type="String" Name="name" DefaultValue="Sajjad" />
</UpdateParameters>
</asp:SqlDataSource>
.cs
protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["name"].Value = "bvhjbjh";
}
but when i press update button occur below error
An SqlParameter with ParameterName 'name' is not contained by this
SqlParameterCollection.
You are missing the # symbol.
The following should work :
protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#name"].Value = "bvhjbjh";
}
For more information on using parameters with sqldatasource check this out.
The problem could be that there is not a parameter in your parameter list with the name "name".
Try:
e.Command.Parameters.Add(new SqlParameter("name", "bvhjbjh"));

ASP.NET [Object data source]

I send id of my class object from one page to second this way :
NavigateUrl='<%# "ItemDetail.aspx?itemId=" + (string)Eval("Id") %>'
Then I get the object with ObjectDataSource and function this way :
<asp:ObjectDataSource ID="ObjectDataSourceItem" runat="server" SelectMethod="GetItem"
TypeName="Catalog">
<SelectParameters>
<asp:QueryStringParameter Name="itemId" QueryStringField="itemId" Type="string" DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
So how to use this item in my new page code :
this.ObjectDataSourceItem.?????
You need to subscribe to the ObjectDataSource's Selected event, access the ObjectDataSourceStatusEventArgs ReturnValue property (e.ReturnValue), and cast it to the appropriate type. Something like:
protected void ObjectDataSourceItem_Selected(object source, ObjectDataSourceStatusEventArgs e)
{
var myDataSet = (DataSet)e.ReturnValue;
}

Resources