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

ChiBellaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.