Solved

Pass selected value to a function

Posted on 2016-09-26
14
42 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 3
14 Comments
 
LVL 52

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 52

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 52

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 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