Improve company productivity with a Business Account.Sign Up

x
?
Solved

Set the Value of a ComboBox nested in a listview

Posted on 2011-02-11
6
Medium Priority
?
781 Views
Last Modified: 2012-08-13
I am new to working with AJAX, but have been working with C# for a few years.

On my ASP.NET page I have a listview and it contains multiple rows with One ComboBox and One Textbox.

The combobox values are populated fine, but I can't figure out how to set the correct value when loading the page. It always goes to index 0 no matter what should be selected.

I tried to do selectedvalue, and selectedIndex etc but they are not available and I can't seem to figure out how to access them.

Any Assistance is appreciated.

<asp:ListView ID="ListView1" runat="server" DataSourceID="dsRecipe" DataKeyNames="MeatChzID"
        InsertItemPosition="LastItem">
        <LayoutTemplate>
            <table>
                <tr>
                    <thead>
                        <th>
                            Description
                        </th>
                        <th>
                            Amount
                        </th>
                    </thead>
                </tr>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:ComboBox ID="cbDescription" runat="server" AutoPostBack="true" DropDownStyle="DropDown"
                        AutoCompleteMode="SuggestAppend" CaseSensitive="false" ItemInsertLocation="Append"
                        DataSourceID="dsMeatChz" DataTextField="MeatChzDescrip" DataValueField="MeatChzID" />
                </td>
                <td>
                    <asp:TextBox ID="tbAmount" runat="server" Text='<%#Bind("Amount") %>' Columns="5"></asp:TextBox>
                </td>
            </tr>
        </ItemTemplate>
</ListView>

Open in new window

0
Comment
Question by:dgerler
6 Comments
 
LVL 13

Accepted Solution

by:
devlab2012 earned 2000 total points
ID: 34872414
you can do that in RowDataBound event.
0
 
LVL 83

Expert Comment

by:leakim971
ID: 34873288
Coul you right clik on the page, choose view source and post it here ? Thanks.
0
 

Author Comment

by:dgerler
ID: 34892520
Here the page source.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Meat & Cheese Recipe Editor</title>
<link href="/Options/WebResource.axd?d=7ybj06IoJZhUxgPO3QZz2bGk40BTgFiI3dHZjqizeTirdrGQ6hcHV_GgCX-1_eUnIFNCX9LP1zoWtmn-xVzsqMd5XetAe0ywG8J6-F8JrnO6zfjjT64QZ1az8AakKLgOaKP4jw2&amp;t=634092630280000000" type="text/css" rel="stylesheet" /><title>
	Untitled Page
</title></head>
<body>
    <form name="aspnetForm" method="post" action="MeatChzRecipeEdit.aspx" id="aspnetForm">
<input type="hidden" name="_ctl0_ToolkitScriptManager1_HiddenField" id="_ctl0_ToolkitScriptManager1_HiddenField" value="" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="RmkLG9G1n+CB512J3e5gbeZaRCUFEI9JvofRR1iG0NOw7pLb492usUszW1etlkUBNpUjjHRcqrebHXBlG60GqqRUAuJTHM4CzT1Lk/6ppytdlyOFYMxLlsVRvJ0stI30CLzRcSGOlGBXKZC9JdKdQjx1fNLCGKzyHm2bd4PDZ/W0MWDXbfngTCTnjXxhwpfrsMiQJ6+PVM2vvrR+cg/Ep63tfs3n4yykrUy6txrnR/sc/N3bmBFMqg5qnNJcDlxmS7eJ6tPHXMtQZzs5Fb1vldSbi5qtlkDHlgV0JIizOIFn94fE8jO+52Qp5wmwIe9ArXnLRbF9vuWDfbFBaQic2y99kmVJwgEaP1J+nJD7jz7KyKReVtsD09C07AElr+h5ZFnSeouD/YjHVCa2RKCBIX2axkKYN+avRRol224LqzZyULbO+2C+WwCRdT3KL65HkW6+FOX6VkBJ8j7LJoQZNFrwwXZ9tZaA0kU4zEPKEhowtJirxQ4xevFIJnjfRDe9ymp/jnXusCGpyA53mQ5kLRT1RDRTraswA+IvVY6xEZmuFI9YWDhv+fOooHZLx0f/NBO3lLPaDBEDQBbXWqsxYwViM4eY1kxcsxSqb5vcfFf7xhQGpdQmDeJEpX4t330vcpOrefKmTniSfjD6nPiFn+jNoqJcsviybrGgFoNcN2czw5n0tT0SOAO88LUvR9rzGArDO8xiE338aF1ucwAi25ZOmnHvVvJoNlX+Trt0uXtPzQhyp36AlmIYi8j/+N/vGdJ108jY0mwQrqi0DY8dspUItur+udsZUgo6QtoKfE3weQV83jRKUwjw1VqGZHjlH3a9bLQ2/hzzcJ02eqXoDL1zDMitqG1J5SIUdXuGd87oWAcOGk2/WM9nEh48tobg2/Zs+A==" />
 
<script type="text/javascript"> 
<!--
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</script>
 
 
 
<script src="/Options/ScriptResource.axd?d=rbm5uPLCArcufOEtjZoRpGS0ZKDxlCR7gj4uRNAOv2TyXBKauuQ7saVMpgSMiSz6mR46_TC7jVPE7ixXr2-buShX-wi77pb6I24uwVtmcoqzL_gRxh9zPzETA7y7p5rHXgyTqA2&amp;t=36465846" type="text/javascript"></script>
<script type="text/javascript"> 
<!--
if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');
// -->
</script>
 
<script src="/Options/MeatChzRecipeEdit.aspx?_TSM_HiddenField_=_ctl0_ToolkitScriptManager1_HiddenField&amp;_TSM_CombinedScripts_=%3b%3bAjaxControlToolkit%2c+Version%3d3.5.40412.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d28f01b0e84b6d53e%3aen-US%3a1547e793-5b7e-48fe-8490-03a375b13a33%3af9cec9bc%3ade1feab2%3af2c8e708%3a720a52bf%3a589eaa30%3a698129cf%3ad9d4bb33" type="text/javascript"></script>
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="0cxRWMF8ttl0FKluczu2hAQVn3KPCNcIO/e2UfVsYRddU2+6IrJdtmPTSQ/fRVKYyamNGFXwZzvcGLtJr4Wqh7/J66M5jSzfvHKOrFj0n5bUO1IkOJ1G88AAmbgms1aQ8VVnJdiSbQ9ldJigfUhsfHmohNTrU25bqv28XF4GMogbMUtT2bvhBjAHtOPdjwU1HH+ViA==" />
    <div>
        
        
    
    
    
    <div onchange="javascript:setTimeout('__doPostBack(\'_ctl0$ContentPlaceHolder1$cbPLU\',\'\')', 0)" language="javascript" id="_ctl0_ContentPlaceHolder1_cbPLU" style="display:inline;">
	<table id="_ctl0_ContentPlaceHolder1_cbPLU_Table" class="ajax__combobox_inputcontainer" cellspacing="0" cellpadding="0" border="0" style="border-width:0px;border-style:None;border-collapse:collapse;display:inline;position:relative;top:5px;">
		<tr>
			<td class="ajax__combobox_textboxcontainer"><input name="_ctl0:ContentPlaceHolder1:cbPLU:TextBox" type="text" value="1" id="_ctl0_ContentPlaceHolder1_cbPLU_TextBox" autocomplete="off" /></td><td class="ajax__combobox_buttoncontainer"><button id="_ctl0_ContentPlaceHolder1_cbPLU_Button" type="button" style="visibility:hidden;"></button></td>
		</tr>
	</table><ul id="_ctl0_ContentPlaceHolder1_cbPLU_OptionList" class="ajax__combobox_itemlist" style="display:none;visibility:hidden;">
		<li>Select or Enter a PLU</li><li>1</li><li>2</li><li>124</li><li>125</li>
	</ul><input type="hidden" name="_ctl0:ContentPlaceHolder1:cbPLU:HiddenField" id="_ctl0_ContentPlaceHolder1_cbPLU_HiddenField" value="1" />
</div>
   
    
            <table>
                <tr>
                    <thead>
                        <th>
                            Description
                        </th>
                        <th>
                            Amount
                        </th>
                    </thead>
                </tr>
                
            <tr>
                <td>
                    <div onchange="javascript:setTimeout('__doPostBack(\'_ctl0$ContentPlaceHolder1$ListView1$ctrl0$cbDescription\',\'\')', 0)" language="javascript" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription" style="display:inline;">
	<table id="_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_Table" class="ajax__combobox_inputcontainer" cellspacing="0" cellpadding="0" border="0" style="border-width:0px;border-style:None;border-collapse:collapse;display:inline;position:relative;top:5px;">
		<tr>
			<td class="ajax__combobox_textboxcontainer"><input name="_ctl0:ContentPlaceHolder1:ListView1:ctrl0:cbDescription:TextBox" type="text" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_TextBox" autocomplete="off" /></td><td class="ajax__combobox_buttoncontainer"><button id="_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_Button" type="button" style="visibility:hidden;"></button></td>
		</tr>
	</table><ul id="_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_OptionList" class="ajax__combobox_itemlist" style="display:none;visibility:hidden;">
		<li>Ham</li><li>Turkey</li><li>Roast Beef</li>
	</ul><input type="hidden" name="_ctl0:ContentPlaceHolder1:ListView1:ctrl0:cbDescription:HiddenField" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_HiddenField" value="0" />
</div>
                </td>
                <td>
                    <input name="_ctl0:ContentPlaceHolder1:ListView1:ctrl0:tbAmount" type="text" value="4.000000" size="5" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl0_tbAmount" />
                </td>
            </tr>
        
            <tr>
                <td>
                    <div onchange="javascript:setTimeout('__doPostBack(\'_ctl0$ContentPlaceHolder1$ListView1$ctrl1$cbDescription\',\'\')', 0)" language="javascript" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription" style="display:inline;">
	<table id="_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_Table" class="ajax__combobox_inputcontainer" cellspacing="0" cellpadding="0" border="0" style="border-width:0px;border-style:None;border-collapse:collapse;display:inline;position:relative;top:5px;">
		<tr>
			<td class="ajax__combobox_textboxcontainer"><input name="_ctl0:ContentPlaceHolder1:ListView1:ctrl1:cbDescription:TextBox" type="text" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_TextBox" autocomplete="off" /></td><td class="ajax__combobox_buttoncontainer"><button id="_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_Button" type="button" style="visibility:hidden;"></button></td>
		</tr>
	</table><ul id="_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_OptionList" class="ajax__combobox_itemlist" style="display:none;visibility:hidden;">
		<li>Ham</li><li>Turkey</li><li>Roast Beef</li>
	</ul><input type="hidden" name="_ctl0:ContentPlaceHolder1:ListView1:ctrl1:cbDescription:HiddenField" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_HiddenField" value="0" />
</div>
                </td>
                <td>
                    <input name="_ctl0:ContentPlaceHolder1:ListView1:ctrl1:tbAmount" type="text" value="3.000000" size="5" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl1_tbAmount" />
                </td>
            </tr>
        
            <tr>
                <td>
                    <input name="_ctl0:ContentPlaceHolder1:ListView1:ctrl2:tbDecsription" type="text" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl2_tbDecsription" />
                </td>
                <td>
                    <input name="_ctl0:ContentPlaceHolder1:ListView1:ctrl2:tbAmount" type="text" size="5" id="_ctl0_ContentPlaceHolder1_ListView1_ctrl2_tbAmount" />
                </td>
            </tr>
        
            </table>
        
 
    </div>
    
 
<script type="text/javascript"> 
<!--
(function() {var fn = function() {$get("_ctl0_ToolkitScriptManager1_HiddenField").value = '';Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();Sys.Application.initialize();
Sys.Application.add_init(function() {
    $create(Sys.Extended.UI.ComboBox, {"autoCompleteMode":3,"autoPostBack":true,"buttonControl":$get("_ctl0_ContentPlaceHolder1_cbPLU_Button"),"comboTableControl":$get("_ctl0_ContentPlaceHolder1_cbPLU_Table"),"dropDownStyle":1,"hiddenFieldControl":$get("_ctl0_ContentPlaceHolder1_cbPLU_HiddenField"),"optionListControl":$get("_ctl0_ContentPlaceHolder1_cbPLU_OptionList"),"selectedIndex":1,"textBoxControl":$get("_ctl0_ContentPlaceHolder1_cbPLU_TextBox")}, null, null, $get("_ctl0_ContentPlaceHolder1_cbPLU"));
});
Sys.Application.add_init(function() {
    $create(Sys.Extended.UI.ComboBox, {"autoCompleteMode":3,"autoPostBack":true,"buttonControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_Button"),"comboTableControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_Table"),"dropDownStyle":1,"hiddenFieldControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_HiddenField"),"optionListControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_OptionList"),"textBoxControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription_TextBox")}, null, null, $get("_ctl0_ContentPlaceHolder1_ListView1_ctrl0_cbDescription"));
});
Sys.Application.add_init(function() {
    $create(Sys.Extended.UI.ComboBox, {"autoCompleteMode":3,"autoPostBack":true,"buttonControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_Button"),"comboTableControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_Table"),"dropDownStyle":1,"hiddenFieldControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_HiddenField"),"optionListControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_OptionList"),"textBoxControl":$get("_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription_TextBox")}, null, null, $get("_ctl0_ContentPlaceHolder1_ListView1_ctrl1_cbDescription"));
});
// -->
</script>
</form>
</body>
</html>

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
LVL 83

Expert Comment

by:leakim971
ID: 34912433
Try this :

PS : do you have a live lnk ?
<script language="javascript" type="text/javascript">
      window.onload = function() { alert("<%= cbDescription.ClientID =>");document.getElementById("<%= cbDescription.ClientID =>").getElementsByTagName("input")[0].value = "";
</script>

Open in new window

0
 

Expert Comment

by:qysan
ID: 34915613
Try to Set it in the Page_Load Event.
Here is a example of a dropdown that I set the Selected Item.

Theo
0
 
LVL 83

Expert Comment

by:leakim971
ID: 34915681
It's not a basic dropdown (asp:DropDownList rendered as a SELECT) but an asp:ComboxBox. It is rendered as a mix of table, input (text and hidden)  and so on...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

584 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