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

Discussion in 'ASP .Net' started by Guest, Jul 13, 2006.

  1. Guest

    Guest Guest

    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
    Guest, Jul 13, 2006
    #1
    1. Advertising

  2. 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 (, 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.
    Walter Wang [MSFT], Jul 13, 2006
    #2
    1. Advertising

  3. Guest

    Guest Guest

    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]" <> wrote in message
    news:...
    > 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 (, 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.
    >
    Guest, Jul 15, 2006
    #3
  4. Guest

    Guest Guest

    All code Re: feeding a SQLDataSource embedded in an .ascx user control a custom property assigned to that control

    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]" <> wrote in message
    news:...
    > 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 (, 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.
    >
    Guest, Jul 15, 2006
    #4
  5. RE: All code Re: feeding a SQLDataSource embedded in an .ascx user control a custom property assigned to that control

    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 (, 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.
    Walter Wang [MSFT], Jul 16, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFyY0RlVm9zLkJF?=

    Incorrect clientID for Calendar control as part of web user contro

    =?Utf-8?B?TWFyY0RlVm9zLkJF?=, Jul 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    =?Utf-8?B?TWFyY0RlVm9zLkJF?=
    Jul 20, 2004
  2. =?Utf-8?B?U3JpZGhhcg==?=
    Replies:
    1
    Views:
    451
    =?Utf-8?B?RWx0b24gVw==?=
    Nov 18, 2005
  3. Philip
    Replies:
    0
    Views:
    126
    Philip
    Jan 29, 2007
  4. whoisnidhee
    Replies:
    0
    Views:
    305
    whoisnidhee
    Jul 15, 2004
  5. Keith Patrick

    ObjectDataSource feeding off a SqlDataSource

    Keith Patrick, Dec 16, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    128
    Keith Patrick
    Dec 16, 2005
Loading...

Share This Page