I need to do a postback on a page, so have got implemented the workaround using the device specific tage etc. However, I now can't bind the data to the dropdown list?!
Imports System.Web.UI.Page
Public Class frm
Inherits System.Web.UI.MobileControls.MobilePage
Protected WithEvents DeviceSpecific1 As System.Web.UI.MobileControls.DeviceSpecific
Public WithEvents DdlCompany As System.Web.UI.WebControls.DropDownList
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet, strError As String
If Not IsPostBack Then
strError = sqlprocs.listGeneric("sp_DataList", ds)
If strError <> "" Then
Trace.Write("Error", strError)
End If
DdlCompany.DataValueField = "Name"
DdlCompany.DataTextField = "Name"
DdlCompany.DataSource = ds
DdlCompany.DataBind()
Well there does not seem to be anything wrong with your code provided your datatextfield and datavaluefield string names are correct which I sure they are and the dataset is not empty.
One thing I did see that is strange is your HTML source markup does not match that of your code behind.
Your drop down list in your HTML markup has an ID of Ddl where as your code behind has declare an drop down list named DdlCompany? You code is also binding to DdlCompany and not Ddl?
Try binding your datasource to Ddl and see what happens? Looks like your markup is not in sync with your code behind
Object reference not set to an instance of an object.
at Mobile.frmSearch.Page_Load(Object sender, EventArgs e) in X:\Mobile\frmSearch.aspx.vb:line 46
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.MobileControls.MobilePage.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()
which is on the line
DdlCompany.DataValueField = "Name"
Have done a trace.write to pullback the data and it returns fine.
Another thing, because you are using templates now you do not have strong typing to your controls.
You will need to find them during runtime and cast them to the control.
//get reference to your contentTemplate
//you can iterate your containing control list to find the device specific
//content template but as an example we will assume it is contained within
//a panel control called myPanel and is the first control in it
Control ctrl = myPanel.Controls[0].FindControl("Ddl");
//cast it to the drop down
DropDownList ddl = (DropDownList)ctrl;
//now you bind your data and key/value this thing control
ddl.Datasource = ds;
ddl.DataTextField = "Name";
ddl.DataValueField = "Name";
Yes, at the code to your code behind before you do assign the datasource
'get reference to your contentTemplate
'you can iterate your containing control list to find the device specific
'content template but as an example we will assume it is contained within
'a form called Form1
dim ddl as DropDownList
dll = Ctype(Form1.Controls(0).FindControl("Ddl"), DropDownList)
//now you bind your data and key/value this dropdown control
ddl.Datasource = ds;
ddl.DataTextField = "Name";
ddl.DataValueField = "Name";
Yes, at the code to your code behind before you do assign the datasource
'get reference to your contentTemplate
'you can iterate your containing control list to find the device specific
'content template but as an example we will assume it is contained within
'a form called Form1
dim ddl as DropDownList
dll = Ctype(Form1.Controls(0).FindControl("Ddl"), DropDownList)
//now you bind your data and key/value this dropdown control
ddl.Datasource = ds;
ddl.DataTextField = "Name";
ddl.DataValueField = "Name";
lynnbob
Member
313 Points
108 Posts
Dropdown lists & Postback!
Oct 24, 2007 11:38 AM|LINK
Hi
I need to do a postback on a page, so have got implemented the workaround using the device specific tage etc. However, I now can't bind the data to the dropdown list?!
This is what I've got...
<mobile:Form id="Form1" runat="server">
<mobile:DeviceSpecific id="DeviceSpecific1" runat="server">
<Choice Filter="supportsJavaScript">
<ContentTemplate>
<asp:DropDownList id="Ddl" runat="server" OnSelectedIndexChanged="Ddl_SelectedIndexChanged" AutoPostBack="True" <asp:DropDownList>
</ContentTemplate></Choice></mobile:DeviceSpecific></mobile:form>
Imports System.Web.UI.Page
Public Class frm
Inherits System.Web.UI.MobileControls.MobilePage
Protected WithEvents DeviceSpecific1 As System.Web.UI.MobileControls.DeviceSpecific
Public WithEvents DdlCompany As System.Web.UI.WebControls.DropDownList
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet, strError As String
If Not IsPostBack Then
strError = sqlprocs.listGeneric("sp_DataList", ds)
If strError <> "" Then
Trace.Write("Error", strError)
End If
DdlCompany.DataValueField = "Name"
DdlCompany.DataTextField = "Name"
DdlCompany.DataSource = ds
DdlCompany.DataBind()
End If
End Sub
Please help!
Lbob
jimmy q
All-Star
54108 Points
3578 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 12:06 PM|LINK
What type of object is ds?
Are you sure ds contains data?
lynnbob
Member
313 Points
108 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 12:14 PM|LINK
jimmy q
All-Star
54108 Points
3578 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 12:45 PM|LINK
Well there does not seem to be anything wrong with your code provided your datatextfield and datavaluefield string names are correct which I sure they are and the dataset is not empty.
One thing I did see that is strange is your HTML source markup does not match that of your code behind.
Your drop down list in your HTML markup has an ID of Ddl where as your code behind has declare an drop down list named DdlCompany? You code is also binding to DdlCompany and not Ddl?
Try binding your datasource to Ddl and see what happens? Looks like your markup is not in sync with your code behind
lynnbob
Member
313 Points
108 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 12:53 PM|LINK
No still no joy I'm afraid, the error I get is
Object reference not set to an instance of an object.
which is on the lineat Mobile.frmSearch.Page_Load(Object sender, EventArgs e) in X:\Mobile\frmSearch.aspx.vb:line 46
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.MobileControls.MobilePage.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()
DdlCompany.DataValueField = "Name"
Have done a trace.write to pullback the data and it returns fine.
I'm baffled?!
jimmy q
All-Star
54108 Points
3578 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 12:55 PM|LINK
That means your DdlCompany is null and not initialised yet.
Put a break point on that line and hover your cursor over the DdlCompany and see whether it is null, or add it to the watch window?
Have you tried using Ddl instead of DdlCompany as your dropdownlist in your markup is referenced as Ddl and NOT DdlCompany.
jimmy q
All-Star
54108 Points
3578 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 12:58 PM|LINK
Another thing, because you are using templates now you do not have strong typing to your controls.
You will need to find them during runtime and cast them to the control.
//get reference to your contentTemplate //you can iterate your containing control list to find the device specific //content template but as an example we will assume it is contained within //a panel control called myPanel and is the first control in it Control ctrl = myPanel.Controls[0].FindControl("Ddl"); //cast it to the drop down DropDownList ddl = (DropDownList)ctrl; //now you bind your data and key/value this thing control ddl.Datasource = ds; ddl.DataTextField = "Name"; ddl.DataValueField = "Name";lynnbob
Member
313 Points
108 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 01:18 PM|LINK
Sorry I'm not sure I understand what you mean? Is this in the code behind page? Also I'm doing it with VB not C#
Lbob
jimmy q
All-Star
54108 Points
3578 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 01:42 PM|LINK
Yes, at the code to your code behind before you do assign the datasource
'get reference to your contentTemplate 'you can iterate your containing control list to find the device specific 'content template but as an example we will assume it is contained within 'a form called Form1 dim ddl as DropDownList dll = Ctype(Form1.Controls(0).FindControl("Ddl"), DropDownList) //now you bind your data and key/value this dropdown control ddl.Datasource = ds; ddl.DataTextField = "Name"; ddl.DataValueField = "Name";jimmy q
All-Star
54108 Points
3578 Posts
Re: Dropdown lists & Postback!
Oct 24, 2007 01:43 PM|LINK
Yes, at the code to your code behind before you do assign the datasource
'get reference to your contentTemplate 'you can iterate your containing control list to find the device specific 'content template but as an example we will assume it is contained within 'a form called Form1 dim ddl as DropDownList dll = Ctype(Form1.Controls(0).FindControl("Ddl"), DropDownList) //now you bind your data and key/value this dropdown control ddl.Datasource = ds; ddl.DataTextField = "Name"; ddl.DataValueField = "Name";