We help IT Professionals succeed at work.

global variable control passing from webForm to class file

jim_bob_jim
jim_bob_jim asked
on
493 Views
Last Modified: 2012-05-05
Hi,

Is it possible to have a variable or a control that is declared in my webForm but I can call from my class file??

I want to be able to pass a value from WebForm1 to my class file.

Im using asp.net c#

any help as always greatly appreciated

J
Comment
Watch Question

CERTIFIED EXPERT

Commented:
when u are creating the object of that class cant u just pass it in the constructor

Author

Commented:
How would I do that??

For example I have a dropDownList control in my WebForm1

How would I pass in the SelectedValue of that DropDownList in my class file??

'" + DropDownList1.SelectedValue + "'   (within a select query in my class file)

Thanks
CERTIFIED EXPERT

Commented:
public class UrClass
{
public string Variable;
public UrClass(string FromWebForm)
{
  Variable = FromWebFOrm;
}
}]

// in the web form

UrClass Object = new UrClass(dropdownlist.SelectedValue.ToString());

Author

Commented:
in my WebForm I have a number of controls that are dynamically driven from my database - WebForm1

I have a dataset that is populated from my class file - class.cs   As follows:

            DataSet ds = new DataSet();
            ds.ReadXmlSchema(@"C:DataSetSchema.xsd");

            OleDbDataAdapter da = new OleDbDataAdapter("select * from table1", myConn);
            da_wc.Fill(ds, DATATABLE_NAME);

I want the query in my class file to be filter the dataset based on the selections made from my WebForm.  I therefore need to be able to pass the values of my asp Controls in my WebForm up to my class.cs file so that the query and the dataset is filtered based on the user selections

            OleDbDataAdapter da = new OleDbDataAdapter("select * from table1 where col1 = '" + DropDownList.SelectedValue = "' and col2 = " + TextBox1.Text + "", myConn);

(where DropDownList1 & TextBox1 are declared and selected in WebForm1)

How can I do this??


            da_wc.Fill(ds, DATATABLE_NAME);
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi thanks for help so far

Im having trouble passing parameter into the class has the function is a DataSet method using the Get definition.

    public static DataSet myDataSet
    {
        get
        {
            OleDbConnection myConn = new OleDbConnection();
            myConn.ConnectionString = "xxx";

             DataSet ds = new DataSet();
            ds.ReadXmlSchema(@"C:DataSetSchema.xsd");

            OleDbDataAdapter da = new OleDbDataAdapter("select * from table1", myConn);
            da_wc.Fill(ds, DATATABLE_NAME);

            return ds;
        }
    }

How would I pass the values from WebForm1 into that function in my class as that does not allow me to pass them in using your method I dont think??

thanks for your help so far

J
CERTIFIED EXPERT

Commented:
can u post the constructor of his class where this function is
CERTIFIED EXPERT

Commented:
u wll have to convert this to a function
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.