Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
In my project I bind a listbox1 with say 60px height. When I select an item (for raising another listbox2, using the selected item) that is beneath the 60px height using vertical scroll bar, the status of the listbox is returning back to the first position thereby the selected item goes unknown for the user. Is there any facility to retain the status of listbox on that selected item itself.
Regards.
 

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
	if (ListBox1.SelectedIndex == 0) 
	{ 
	
	} 
	else 
	{ 
		SqlDataAdapter da1 = new SqlDataAdapter("select block_name,block_code from block where dist_code='" + v_dist_code + "'", con); 
		SqlCommandBuilder cb1 = new SqlCommandBuilder(da1); 
		DataSet ds1 = new DataSet("block"); 
		da1.Fill(ds1, "block"); 
		for (int i = 0; i < ds1.Tables[0].Rows.Count; i++) 
		{ 
			List adds1 = new List(); 
			adds1.Add(ds1.Tables[0].Rows[i].ItemArray[0].ToString()); 
			string name1 = ds1.Tables[0].Rows[i]["block_name"].ToString(); 
			if (name1 == ListBox1.SelectedValue) 
			{ 
				v_block_code = Convert.ToInt32(ds1.Tables[0].Rows[i]["block_code"].ToString()); 
			}
				
		} 
	}
	
// once an item is selected the position of listbox goes to top and things happens as reported.
 
[EDIT]David_Wimbley: Moved code from comment to question[/EDIT]
 
// This problem only with VS2008 R2 and not with VS2008!
Posted 16 Jan '13 - 21:27
Edited 19 Jan '13 - 1:48

Comments
ram_kumar - 17 Jan '13 - 3:31
hey can u post your code then we will do better for you. otherwise you can dynamic add height on selectedchangeindex its maintain your height.
Mohd. Mukhtar - 17 Jan '13 - 3:35
What have you tried so for? Please copy some code snippet here.
vinodkumarnie - 17 Jan '13 - 4:55
It seems easy to resolve if you Post your code. So please post code.
S.Rajendran from Coimbatore - 17 Jan '13 - 4:56
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { if (ListBox1.SelectedIndex == 0) { } else { SqlDataAdapter da1 = new SqlDataAdapter("select block_name,block_code from block where dist_code='" + v_dist_code + "'", con); SqlCommandBuilder cb1 = new SqlCommandBuilder(da1); DataSet ds1 = new DataSet("block"); da1.Fill(ds1, "block"); for (int i = 0; i < ds1.Tables[0].Rows.Count; i++) { List adds1 = new List(); adds1.Add(ds1.Tables[0].Rows[i].ItemArray[0].ToString()); string name1 = ds1.Tables[0].Rows[i]["block_name"].ToString(); if (name1 == ListBox1.SelectedValue) { v_block_code = Convert.ToInt32(ds1.Tables[0].Rows[i]["block_code"].ToString()); } } - once an item is selected the position of listbox goes to top and things happens as reported.
richcb - 17 Jan '13 - 16:04
You need to use the "Improve Question" widget to add the code to your thread.
S.Rajendran from Coimbatore - 19 Jan '13 - 7:47
The problem happens only when I use VS 2008 R2. In VS2008 no problem. Today I found it.
S.Rajendran from Coimbatore - 19 Jan '13 - 21:42
Well, there is a correction. It is not VS 2008 R2..it is VS2008 team system.

1 solution

Please try this..
<script src="jquery-1.4.2.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    $('#btnRight').click(function(e) {
        var selectedOpts = $('#lstBox1 option:selected');
        if (selectedOpts.length == 0) {
            alert("Nothing to move.");
            e.preventDefault();
        }
 
        $('#lstBox2').append($(selectedOpts).clone());
        $(selectedOpts).remove();
        e.preventDefault();
    });
 
    $('#btnLeft').click(function(e) {
        var selectedOpts = $('#lstBox2 option:selected');
        if (selectedOpts.length == 0) {
            alert("Nothing to move.");
            e.preventDefault();
        }
 
        $('#lstBox1').append($(selectedOpts).clone());
        $(selectedOpts).remove();
        e.preventDefault();
    });
});
    </script>
<table style='width: 370px;'>
            <tr>
                <td style='width: 160px;'>
                    <b>Group 1:</b><br />
                    <select multiple="multiple" id='lstBox1'>
                        <option value="ajax">Ajax</option>
                        <option value="jquery">jQuery</option>
                        <option value="javascript">JavaScript</option>
                        <option value="mootool">MooTools</option>
                        <option value="prototype">Prototype</option>
                        <option value="dojo">Dojo</option>
                    </select>
                </td>
                <td style='width: 50px; text-align: center; vertical-align: middle;'>
                    <input type='button' id='btnRight' value='  >  ' />
                    <br />
                    <input type='button' id='btnLeft' value='  <  ' />
                </td>
                <td style='width: 160px;'>
                    <b>Group 2: </b>
                    <br />
                    <select multiple="multiple" id='lstBox2'>
                        <option value="asp">ASP.NET</option>
                        <option value="c#">C#</option>
                        <option value="vb">VB.NET</option>
                        <option value="java">Java</option>
                        <option value="php">PHP</option>
                        <option value="python">Python</option>
                    </select>
                </td>
            </tr>
        </table>
 
Hope it work well...
 
Thank you..
  Permalink  
Comments
Marcus Kramer - 11 Feb '13 - 17:13
Plagiariazed from: Source. When you copy and paste a solution from another source, you must provide a link to that source. As this is the first time, I'm not going to downvote your report the answer, but rather just let you know what's expected. Cheers.
vinodkumarnie - 11 Feb '13 - 23:06
sure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,386
1 OriginalGriff 6,596
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid