?
Solved

Pass selected value to a function

Posted on 2016-09-26
14
Medium Priority
?
45 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 53

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 53

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 53

Accepted Solution

by:
Ryan Chong earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

801 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