Solved

Pass selected value to a function

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

16 Experts available now in Live!

Get 1:1 Help Now