By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,670 Members | 1,526 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,670 IT Pros & Developers. It's quick & easy.

Dropdownlist connect using Ajax when postback reset to first list

P: 4
Hi
I have a problem on Dropdownlist that connect database
and retrieve data by Ajax.I write javascript like this

-------------------------------------------------------------------------
function ManageYear_onchange(machinetype)
{

var iManageYear=document.forms[0].ddlManageYear.selectedIndex;
var vManageYear=document.forms[0].ddlManageYear[iManageYear].value;
clearDll("ddlEquipmentID");
clearDll("ddlVendor");
if(iManageYear!=0)
{
AjaxFile.getEquipmentid(vManageYear,machinetype, GetEquipmentRecords_CallBack);
}
else
{
document.getElementById("ddlEquipmentID").selected Index=0;
document.getElementById("ddlVendor").selectedIndex =0;
return
}

}

function GetEquipmentRecords_CallBack(response)
{
var ds = response.value;
var i = 0;
var iLength = 0;
if (response.error != null){
return;
}
if ((ds.Tables != null) && (ds.Tables[0].Rows.length > 0))
{
//insert data to dropdown
for(i=0; i<ds.Tables[0].Rows.length; i++)
{
document.getElementById("ddlEquipmentID").options. add(new Option(ds.Tables[0].Rows[i]["EquipmentName"],ds.Tables[0].Rows[i]["EquipmentID"]))
}
}
}

function clearDll(ddlname)
{
iLength=document.getElementById(ddlname).options.l ength;
for(i=0; i<iLength; i++)
{
document.getElementById(ddlname).options[0] = null;
}
document.getElementById(ddlname).options.add(new Option("--Please select--",0));

}
-------------------------------------------------------------------------
and i insert ManageYear_onchange() at code behind like this

-------------------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Ajax.Utility.RegisterTypeForAjax(GetType(AjaxFile) )
If Not Me.IsPostBack Then
Me._MachineType = Request.QueryString("MachineType")
ddlManageYear.SelectedValue = clsUtil.DateTimeNow.Year
ddlManageYear.Attributes.Add("onchange", "return ManageYear_onchange('" & Me._MachineType & "');")
Me.RegisterStartupScript("startup", "<script> ManageYear_onchange('" & Me._MachineType & "');</script>")
End If
End Sub
-------------------------------------------------------------------------

on Ajax page i connect to server and send dataset like this

-------------------------------------------------------------------------
<Ajax.AjaxMethod()> _
Public Function getEquipmentid(ByVal ManageYear As Integer, ByVal Machinetype As String) As DataSet
Dim ds As New DataSet
Dim aclsQuery As New clsDBSQLBased
Dim selectQuery As String


selectQuery = "select distinct cast(e.EquipmentId as varchar) + ' : ' + e.[description] [EquipmentName],e.EquipmentId [EquipmentID] from Eim_Equipment e,Eit_ManageEquipment m " + _
" where e.equipmentid=m.equipmentid and " & _
" m.ManageYear=" & ManageYear & _
" and e.MachineType='" & Machinetype & "'"
ds = aclsQuery.GetDataSet(selectQuery)
Return ds

End Function
-------------------------------------------------------------------------

The error is when i click submit button
after i was selected value from dropdown
and postback to server .
The selectedvalue is reset to the first data of dropdown(selectindex =0)
I try to search and debug code (and debug javascript).
Nothing wrong and now i don't know what to do if you can help me
It's very helpful.Thanks.

NC.
Mar 28 '07 #1
Share this Question
Share on Google+
3 Replies


P: 4
Hi
I am confuse about Ajax work. I have an javascript onchange event in dropdown that connect to server using Ajax.like this

Dropdown1 --> onchange( i clear Dropdown2,Dropdown3 and get data use Ajax to Dropdown2 )

Dropdown2 --> onchange ( i clear Dropdown3 and get data use Ajax to Dropdown3 )

After i select all Dropdown (1,2,3).
i click submit button that will save the data of dropdowns to database
when i click button the value of Dropdown2,Dropdown3 is disappear.
I think before back to server the onchange of Dropdown1 is working and clear data of Dropdown2,Dropdown3 .am i wrong? or this is a bug of Ajax?
Am i must save the data of dropdown to hidden textbox before send to server?

Someone pls explain me about Ajax's working.Thanks

NC.
Mar 29 '07 #2

acoder
Expert Mod 15k+
P: 16,027
Post your code or provide a link so we can see the problem.
Mar 29 '07 #3

P: 4
Ok I use Ajax.dll from this site
http://ajax.schwarz-interactive.de/csharpsample/default.aspx
my code :
I have 2 dropdownlists.
1.ddlManageYear
2.ddlEquipmentID

ddlManageyear changed --> retrived data to ddlEquipmentID

[Javascript code in ATM.aspx] ________________________________

function ManageYear_onchange(machinetype)
{
//get selectedvalue from ddlManageyear
var iManageYear=document.forms[0].ddlManageYear.selectedIndex;
var vManageYear=document.forms[0].ddlManageYear[iManageYear].value;
clearDll("ddlEquipmentID");
if(iManageYear!=0)
{

AjaxFile.getEquipmentid(vManageYear, GetEquipmentRecords_CallBack);
}
else
{

document.getElementById("ddlEquipmentID").selected Index=0;
return
}

}
function clearDll(ddlname)
{
iLength=document.getElementById(ddlname).options.l ength;
for(i=0; i<iLength; i++)
{
document.getElementById(ddlname).options[0] = null;
}
document.getElementById(ddlname).options.add(new Option("--Please select--",0));

}

[Codebehind in ATM.aspx] ________________________________
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Ajax.Utility.RegisterTypeForAjax(GetType(AjaxFile) )
If Not Page.IsPostBack Then
loadDropDown() 'Retrieve data to ddlManageyear
ddlManageYear.SelectedValue = clsUtil.DateTimeNow.Year 'set ddlManageyear with default year
ddlManageYear.Attributes.Add("onchange", "ManageYear_onchange();")
Me.RegisterStartupScript("startup", "<script> ManageYear_onchange();</script>")
'when ddlManageYear changed ddlEquipmentID will retrieve data
End If
End Sub


[Code in AjaxFile.aspx ] ___________________________________
<Ajax.AjaxMethod()> _
Public Function getEquipmentid(ByVal ManageYear As Integer) As DataSet
.... retrieve data to ds ....
Return ds
End Function


Problem is that when ddlManageYear has changed ddlEquipmentid will retrieve data it's correct but when click submit button to save data
it don't have any data....
Please help
Mar 30 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.