Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
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.


C#
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
Updated 19-Jan-13 1:48am
v3
Comments
Ram Kumar(Webunitech) 17-Jan-13 3:31am    
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:35am    
What have you tried so for? Please copy some code snippet here.
vinodkumarnie 17-Jan-13 4:55am    
It seems easy to resolve if you Post your code. So please post code.
S.Rajendran from Coimbatore 17-Jan-13 4:56am    
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<string> adds1 = new List<string>();
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.
Richard C Bishop 17-Jan-13 16:04pm    
You need to use the "Improve Question" widget to add the code to your thread.

1 solution

Please try this..
XML
<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>

XML
<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..
 
Share this answer
 
Comments
fjdiewornncalwe 11-Feb-13 17:13pm    
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:06pm    
sure

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900