• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 874
  • Last Modified:

LinqDataSource within User Control - how to pass parameters from aspx to control?

I want to set the where parameters of a linqdatasource with values from properties set when the user control is loaded (passed from the calling aspx page).

 I tried using the parameter option within whereparameters but this didn't work. How can I set the datasource where parameters with values from properties set within code behind.

 


<asp:LinqDataSource ID="LinqDataNotes1" runat="server"
                      ContextTypeName="Example.SomeDataContext"
                      Select="new (someinfo)"
                      Where='NAME=@name AND ID= @ID' TableName="SomeTable">
                   <WhereParameters>
                    <asp:Parameter
                     Name="ID"
                     DefaultValue="0"                      
                     Type="Int32" />
                     <asp:Parameter
                     Name="name"                
                     DefaultValue=""
                      Type="String"/>
                   </WhereParameters>
                    
                  </asp:LinqDataSource>

Open in new window

0
ChiBella
Asked:
ChiBella
  • 4
  • 2
1 Solution
 
jandromedaCommented:
When you create a control in the aspx its id becomes the identifier of the object and you can use it in the code behind to set its parameter.

<asp:LinqDataSource ID="LinqDataNotes1".....

code behind

LinqDataNotes1.<Propertyx> = <valuey>;
0
 
ChiBellaAuthor Commented:
That is not what I am asking...but thanks....

What I want to know is how to pass a parameter from the web page to the control page user {get set} or some other method so that I can set the where clause of a LinqDataSource.

Maybe I'm missing something in your response. Can you explain how to set the where clause from passed parameters?
0
 
jandromedaCommented:
Do you want to get a parameter to this page where you have the datasource from another page? If so you can use query string parameters.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
ChiBellaAuthor Commented:
Since this is a user control and I can set properties on the user control from the page the user control is used on, isn't there a better way?  

If I set public properties on the page can I get to them?
0
 
ChiBellaAuthor Commented:
I will use this control several times within a page -- each time I will set a different parameter value for the where clause...which will populate several grids with different queries on the page.

I must be missing something basic on how to set the parameters on a linqdatasource????

Can anyone steer me in the right direction?
0
 
ChiBellaAuthor Commented:
ok...I found my own solution.

Setting AutoGenerateWhereClause=true and capture the property values for the where parameters within the OnSelecting event does it!

 


<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
                   AutoGenerateWhereClause= "true"
                      ContextTypeName="Some.METSDataContext" 
                      Select="new (SOME_DATA)" 
                      TableName="SOMETABLEs"
                       OnSelecting="LinqDataSource1_Selecting">
                   <WhereParameters>
                   <asp:Parameter
                     Name="SOME_ID"
                     DefaultValue="0"                       
                     Type="Int32" /> 
                     <asp:Parameter
                     Name="SOME_NAME"                 
                     DefaultValue=""
                      Type="String"/>
                   </WhereParameters>
                     
                  </asp:LinqDataSource>
 
code behind:
 
private Int32 someId;
private string somename;
 
 
    public Int32 SomeId
    {
 
        get { return someId; }
        set { someId = value; }
 
    }
 
    public string Somename
    {
        get { return somename; }
        set { somename = value; }
 
    }
 
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        e.WhereParameters["SOME_ID"] = someId;
        e.WhereParameters["SOME_NAME"] = somename;
    
    }

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now