473,390 Members | 827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

feeding a SQLDataSource embedded in an .ascx user control a custom property assigned to that control

I'm building some user controls. I very much like how you can build custom
properties to be bound to a user control, and how instances of that control
will show those custom properties in the VS.NET IDE.

I've made a user control -- let's call it MyUserControl.ascx -- with a
DataGrid and an SqlDataSource. The SqlDataSource that loads content by
categoryID.

I've defined a property on MyUserControl.ascx called ContentCategoryID.

I want custom property ContentCategoryID to be the parameter that filters
SqlDataSource. Can anyone tell me what is the cleanest/best way to do this?

Thanks,
-KF
Jul 12 '06 #1
4 3429
Hi,

Thank you for your post.

Based on my understanding, your question is how to pass the UserControl's
property to its SqlDataSource as one of its select parameters. If I've
misunderstood anything, please feel free to post here.

I recommend you handle the SqlDataSource's Selecting event and set the
select parameter value there. Also, you need to call DataGrid.DataBind()
after the UserControl's property gets changed.

I'm using SqlServer NorthWind database "Order Details" as an example:

<asp:DataGrid ID="grid1" runat="server"
DataSourceID="SqlDataSource1"></asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Order Details] WHERE ([OrderID] =
@OrderID)" OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter Name="OrderID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
public int OrderID
{
get
{
object o = ViewState["OrderID"];
if (o == null) return 0;
return (int) o;
}
set
{
ViewState["OrderID"] = value;
grid1.DataBind();
}
}
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@OrderID"].Value = OrderID;
}

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 13 '06 #2
Walter,

Your post gave me the information I needed to accomplish what I set out to
do, which was to develop a generic control to list administrative overviews,
and to call different content categories based on a custom parameter fed in
through the IDE. Thanks as always for your comprehensive help.

Anyone following in my footsteps is encouraged to be mindful of the
distinctions between SqlDataSource <SelectParameters-- there are different
types ("Parameter", "CommandParameter") and if you sub in the wrong one from
older example code, things will break

Thanks again for your help.

-KF

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:bu**************@TK2MSFTNGXA01.phx.gbl...
Hi,

Thank you for your post.

Based on my understanding, your question is how to pass the UserControl's
property to its SqlDataSource as one of its select parameters. If I've
misunderstood anything, please feel free to post here.

I recommend you handle the SqlDataSource's Selecting event and set the
select parameter value there. Also, you need to call DataGrid.DataBind()
after the UserControl's property gets changed.

I'm using SqlServer NorthWind database "Order Details" as an example:

<asp:DataGrid ID="grid1" runat="server"
DataSourceID="SqlDataSource1"></asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Order Details] WHERE ([OrderID] =
@OrderID)" OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter Name="OrderID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
public int OrderID
{
get
{
object o = ViewState["OrderID"];
if (o == null) return 0;
return (int) o;
}
set
{
ViewState["OrderID"] = value;
grid1.DataBind();
}
}
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@OrderID"].Value = OrderID;
}

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Jul 15 '06 #3
This is the full code for what I described in my earlier scenario for anyone
trying to duplicate what I've done. Note also that the DataBind() method
needs to go in the page load event.

In the .ascx:
<asp:SqlDataSource OnSelecting="SqlDataSource1_Selecting"
ID="SqlDataSource1" runat="server" ConnectionString="<%$ [redacted for
security, whatever references your connection string] %>"
SelectCommand="SELECT [Con_ContentID], [Sta_StatusID], [Con_InsertDate],
[Con_PubDate], [Con_Title], [Con_Body], [Con_Url], [Con_Icon] FROM
[Contentitems] WHERE ([Con_CategoryID] = @Con_CategoryID)">
<SelectParameters>
<asp:Parameter Name="Con_CategoryID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

In the codebehind for the .ascx:
public partial class AdminControls_OverviewByContentCategory :
System.Web.UI.UserControl
{

private int contentcategoryid;

public int ContentCategoryID
{
get
{
return contentcategoryid;
}
set
{
contentcategoryid = value;

}
}

protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@Con_CategoryID"].Value = ContentCategoryID;
}

protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataBind();
}

}

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:bu**************@TK2MSFTNGXA01.phx.gbl...
Hi,

Thank you for your post.

Based on my understanding, your question is how to pass the UserControl's
property to its SqlDataSource as one of its select parameters. If I've
misunderstood anything, please feel free to post here.

I recommend you handle the SqlDataSource's Selecting event and set the
select parameter value there. Also, you need to call DataGrid.DataBind()
after the UserControl's property gets changed.

I'm using SqlServer NorthWind database "Order Details" as an example:

<asp:DataGrid ID="grid1" runat="server"
DataSourceID="SqlDataSource1"></asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Order Details] WHERE ([OrderID] =
@OrderID)" OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter Name="OrderID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
public int OrderID
{
get
{
object o = ViewState["OrderID"];
if (o == null) return 0;
return (int) o;
}
set
{
ViewState["OrderID"] = value;
grid1.DataBind();
}
}
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@OrderID"].Value = OrderID;
}

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Jul 15 '06 #4
Hi,

Thank you for your update and sample code. I'm sure that this will benefit
the community a lot.

Have a nice day!
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephen | last post by:
Hey everyone. I was wondering if someone could help me with a small problem. I have designed a user control and I would have inserted it on a aspz page (WebForm1). The User control is being used to...
5
by: djscratchnsniffing | last post by:
i know you can access an ascx's properties/methods from an aspx file. Let's say you have an aspx file with two code-behind files(ascx files). Can you access one of the ascx file's...
6
by: David Bowey | last post by:
Hi There! I have som reusable ASCX controls that I also want to let my clients use in their websites. However, I don't want them to "have" the ASCX controls on their web servers. Instead I would...
0
by: ElSchepo | last post by:
Hi, Just read http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/customcontrolfromusercontrol.asp on how to put User Controls (ascx) into an assembly. I made a...
2
by: ryan.mclean | last post by:
Hello all, Can I add custom verbs to ascx webparts? I have seen good examples where the webpart is a custom control thingy that inherits from System.Web.UI.WebControls.WebParts, then overrides...
7
by: | last post by:
I'm using ASP.NET 2.0, C#, SQL Server 2005, and databound controls like the DataList and the GridView. Right now I'm using SqlDataSource as part of very quick and dirty site; I'm not looking to...
6
by: Ken Fine | last post by:
I'm using SQLDataSource, which generates some kind of dataset, and then I attach that datasource to various data display controls such as DataList and repeater which loop through to the end of the...
5
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Prior to C# 3.0, I would typically define a property of a custom user control with pattern A: === PATTERN A ========== private bool myBool = true; public bool MyBool { get { return myBool; }...
2
by: =?Utf-8?B?VkIuTmV0IFBybw==?= | last post by:
I have a user control (.ascx) with a combo box in it. I place it and a SQLDataSource control on a web form. I want a SQLDataSource parameter to point to the combo box, but it seems that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.