Solved

Pass selected value to a function

Posted on 2016-09-26
14
38 Views
Last Modified: 2016-09-28
Hi,

I have a ListBox that reads values from a table. When user clicks on an item in that Listbox, is there a way to pass the DataTextField, and DataTextField to a function?  

This is the code I have, it calls the function, but not the parameters.

                <asp:ListBox ID="listBoxReports" runat="server" SelectionMode="Multiple" width="150" height="150" AutoPostBack="True" OnSelectedIndexChanged="LoadIndividualReport">
                            <asp:ListItem Value=""></asp:ListItem>
                </asp:ListBox> 

Open in new window



thanks
0
Comment
Question by:mcrmg
  • 8
  • 3
  • 3
14 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41817002
listitem is containing value and text. which value u want to pass to a function?
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 41817213
You can pass listBoxReports.SelectedItem.ToString()
0
 

Author Comment

by:mcrmg
ID: 41817747
I would like to pass both value and text to the function.  here is my function

    protected void LoadIndividualReport(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
        con.Open();

        SqlCommand SqlCom = con.CreateCommand();
        SqlCom.CommandText = "Execute st_test @ReportID";
        SqlCom.Parameters.Add("@ReportID", System.Data.SqlDbType.Int).Value = ??

        SqlDataReader reader = SqlCom.ExecuteReader();

        if (reader.Read())
        {


            testBox.Text = ??? //Value
            testBox1.Text = ??? //Text
            
        }




        con.Close();




    }

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41817851
when will you call the method: LoadIndividualReport ? during a postback?
0
 

Author Comment

by:mcrmg
ID: 41817864
When user clicks on an item on the list. thanks
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 41817885
testBox.Text =  e.SelectedValue;
            testBox1.Text = e.SelectedItem.ToString();
0
 

Author Comment

by:mcrmg
ID: 41817958
I am getting this error:


CS1061: 'System.EventArgs' does not contain a definition for 'SelectedItem' and no extension method 'SelectedItem' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 41818221
Try with following:

listBoxReports.SelectedItem.ToString()
listBoxReports.SelectedValue

https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged(v=vs.110).aspx
0
 

Author Comment

by:mcrmg
ID: 41818228
testBox.Text = listBoxReports.DataTextField;
            testBox.Text = listBoxReports.DataValueField;

is showing the field name for text and value, not the real value. thanks
0
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41819025
you can have something like this:

<asp:DropDownList ID="listBoxReports" runat="server" AutoPostBack="True"
            DataSourceID="SqlDataSource1" DataTextField="yourtextfield"
            DataValueField="yourvaluefield"
            onselectedindexchanged="listBoxReports_SelectedIndexChanged">
        </asp:DropDownList>

then in code behind:
protected void listBoxReports_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadIndividualReport(sender, e);
        }

protected void LoadIndividualReport(object sender, EventArgs e)
        {
            DropDownList d = (DropDownList)sender;
            if (d.SelectedItem != null)
            {
                testBox.Text = d.SelectedItem.Text;
                testBox1.Text = d.SelectedItem.Value;
            }
        }

Open in new window

0
 

Author Comment

by:mcrmg
ID: 41819670
Thanks for the quick reply. I am getting this error after applying your sample code.  thanks


System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.ListBox' to type 'System.Web.UI.WebControls.DropDownList'.
0
 

Author Comment

by:mcrmg
ID: 41819755
Here is my progress so far.  I get to a point that I can pass the text/value to a function (with your code). For some reason, it does not matter what item I selected from the dropdown list, it always return the first item.  Any idea?  thanks
0
 

Author Comment

by:mcrmg
ID: 41819909
if I added a button

<asp:Button id="Fetchbtn" onclick="LoadIndividualReport" runat="server" text="Fetch" />  

instead of using

AutoPostBack="True" onselectedindexchanged="LoadIndividualReport"


I am getting error as following:

 System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.DropDownList'.
0
 

Author Closing Comment

by:mcrmg
ID: 41819991
It is working now..thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question