Solved

Pass selected value to a function

Posted on 2016-09-26
14
30 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 49

Expert Comment

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

Expert Comment

by:Ramkisan Jagtap
Comment Utility
You can pass listBoxReports.SelectedItem.ToString()
0
 

Author Comment

by:mcrmg
Comment Utility
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
 
LVL 49

Expert Comment

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

Author Comment

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

Expert Comment

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

Author Comment

by:mcrmg
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 12

Expert Comment

by:Ramkisan Jagtap
Comment Utility
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
Comment Utility
testBox.Text = listBoxReports.DataTextField;
            testBox.Text = listBoxReports.DataValueField;

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

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It is working now..thank you
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now