Solved

Set the Value of a ComboBox nested in a listview

Posted on 2011-02-11
6
718 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 500 total points
ID: 34872414
you can do that in RowDataBound event.
0
 
LVL 82

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 82

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 82

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

What Is Threat Intelligence?

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

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

12 Experts available now in Live!

Get 1:1 Help Now