selecting radio button in datalist

V

Vikas Kumar

i am selectin some data from database and through reader showing it in
datalist as above now for radio buttons i have single column which returns
1,2,3 and according to that i want one of above radio button to be
preselected in this datalist how can i do that
doing something like this
but this gives error "The type or namespace name 'rdbtn' could not be found
(are you missing a using directive or an assembly reference?)"
as its in datalist



<table width="100%">
<tr width="100%">
<td width="25%"><%#DataBinder.Eval(Container.DataItem,"FName")%></td>
<td width="25%"><input type=text name="txtPass"
value='<%#DataBinder.Eval(Container.DataItem,"Passwd")%>'></td>
<td width="45%" align="left">





<input type=hidden name="txtPerm"
value='<%#DataBinder.Eval(Container.DataItem,"Permission")%>'>
<%string x=Request.Form.Get("txtPerm");%>
<% if(x=="1")
{
rdbtn.SelectedValue="1";
}
else if(x=="2")
{
rdbtn.SelectedValue="2";
}
else if(x=="3")
{
rdbtn.SelectedValue="3";
}%>
</td>
<td width="5%" align="left">
</td>
</tr>
</table>
 
S

Steven Cheng[MSFT]

Hi Vikas,

Welcome to the ASPNET newsgroup.

From your description, I understand you're developing asp.net web page
which use a datalist control to populate some datas from database. And the
datalist's template contains some radiobuttons, the radiobutton's data is
populated from data object through databinding. Currently you're
encountering some problem on preselect the certain radio button according
to the data object's certain field. correct?

Based on my experience, for such scenario, you can consider either using
helper function or the ItemDataBound event to customize the controls in
template:

e.g:

1. we can define a helper function and use it for setting the radiobutton's
selection status


<asp:RadioButton ID="rb1" runat="server" GroupName="gp1" Checked='<%#
SetChecked("rb1", Container.DataItem) %>' />
<asp:RadioButton ID="rb2" runat="server" GroupName="gp1" Checked='<%#
SetChecked("rb2", Container.DataItem) %>' />


========a protected function in codebehind page class==========
protected bool SetChecked(string rbid, object obj)
{
DataRowView drv = obj as DataRowView;

if (rbid == "rb1")
{
return ((int)(drv[0]) % 2 == 0);
}

return !((int)(drv[0]) % 2 == 0);

}



2. Also, we can use "ItemDataBound" event to get the radioButton control in
the template and manipulate them:

protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");
RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

// code to set check status....

}


BTW, in the code snippetd you posted, I found that you're using the inline
data rendering expression( <% xxxxx %> ) in the DataList's template, I
don't think this is the recommended means, and <% %> is supposed to be used
in the top page template, and in databound control's template, we're always
recommended to use databinding expression( <%# %>)

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
V

Vikas Kumar

Hi thanks for reply yes u got the problem correct
i want to use
ItemDataBound event to customize the controls in
template

i had coded like this

<ItemTemplate>
<table width="100%">
<tr width="100%">
<td
width="25%"><%#DataBinder.Eval(Container.DataItem,"FName")%></td>
<td width="25%"><input type=text name="txtPass"
value='<%#DataBinder.Eval(Container.DataItem,"Passwd")%>'></td>
<td width="45%" align="left">
<asp:RadioButton ID="rb1" runat="server" GroupName="gp1"
/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:RadioButton ID="rb2" runat="server" GroupName="gp1"
/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:RadioButton ID="rb3" Runat="server" GroupName="gp1"
/>

</td>
<td width="5%" align="left">
<asp:Button ID="EditSubuser" Text="Edit"
Runat="server"></asp:Button></td>
</tr>
</table>
</ItemTemplate>

and in code behind
protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");

RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");


}

its working fine till here
but how will i select one of these three now
the code below selects 1,2 or 3 from permission colomn
how will i take value from permission coloumn and will set one of these
three rd1 or rd2 or rd3

string strsql="select
Userinfo.UID,Userinfo.FName,Userinfo.Permission,UserPassword.Passwd from
Userinfo,UserPassword where Userinfo.UID=UserPassword.UID and
Userinfo.ParentID="+ userid;

Database db=DatabaseFactory.CreateDatabase();

DBCommandWrapper cmd=db.GetSqlStringCommandWrapper(strsql);

dr=db.ExecuteReader(cmd);

listUser.DataSource=dr;

listUser.DataBind();

dr.Close();



Steven Cheng said:
Hi Vikas,

Welcome to the ASPNET newsgroup.

From your description, I understand you're developing asp.net web page
which use a datalist control to populate some datas from database. And the
datalist's template contains some radiobuttons, the radiobutton's data is
populated from data object through databinding. Currently you're
encountering some problem on preselect the certain radio button according
to the data object's certain field. correct?

Based on my experience, for such scenario, you can consider either using
helper function or the ItemDataBound event to customize the controls in
template:

e.g:

1. we can define a helper function and use it for setting the
radiobutton's
selection status


<asp:RadioButton ID="rb1" runat="server" GroupName="gp1" Checked='<%#
SetChecked("rb1", Container.DataItem) %>' />
<asp:RadioButton ID="rb2" runat="server" GroupName="gp1" Checked='<%#
SetChecked("rb2", Container.DataItem) %>' />


========a protected function in codebehind page class==========
protected bool SetChecked(string rbid, object obj)
{
DataRowView drv = obj as DataRowView;

if (rbid == "rb1")
{
return ((int)(drv[0]) % 2 == 0);
}

return !((int)(drv[0]) % 2 == 0);

}



2. Also, we can use "ItemDataBound" event to get the radioButton control
in
the template and manipulate them:

protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");
RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

// code to set check status....

}


BTW, in the code snippetd you posted, I found that you're using the inline
data rendering expression( <% xxxxx %> ) in the DataList's template, I
don't think this is the recommended means, and <% %> is supposed to be
used
in the top page template, and in databound control's template, we're
always
recommended to use databinding expression( <%# %>)

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no
rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Thanks for your response Vikas,

Since you've got the DataRowView object in ItemDataBound event, then you
can query the certain column(select through dataReader in database) value
from it, e.g:

=================================
protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

DataRowView drv = e.Item.DataItem as DataRowView;


int selval = (int)drv["selectvalue"];

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");

RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");


}
====================

DataRowView is just somewhat like DataRow, you can access those database
column's value through index or name:

#DataRowView Class
http://msdn2.microsoft.com/en-us/library/system.data.datarowview(VS.80).aspx

Then, you can set certain RadioButton's Checked property to false or true
based on the column's value.

Hope this helps. If you still have anything unclear, please feel free to
post here.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
V

Vikas Kumar

I had already tried thid code
protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");

RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");

int setperm = (int)drv["Permission"];


i am getting following error


--------------------------------------------------------------------------------


Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 80: RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");
Line 81: RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");
Line 82: int setperm = (int)drv["Permission"];
Line 83:
Line 84:


Source File: d:\ankit\property\user\manageuser.aspx.cs Line: 82

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
property.user.manageuser.listUser_ItemDataBound(Object sender,
DataListItemEventArgs e) in d:\ankit\property\user\manageuser.aspx.cs:82
System.Web.UI.WebControls.DataList.OnItemDataBound(DataListItemEventArgs
e)
System.Web.UI.WebControls.DataList.CreateItem(Int32 itemIndex,
ListItemType itemType, Boolean dataBind, Object dataItem)
System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean
useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
property.user.manageuser.Page_Load(Object sender, EventArgs e) in
d:\ankit\property\user\manageuser.aspx.cs:48
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


and same error i get if i write rb1.Checked=true;
for rb1
Steven Cheng said:
Thanks for your response Vikas,

Since you've got the DataRowView object in ItemDataBound event, then you
can query the certain column(select through dataReader in database) value
from it, e.g:

=================================
protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

DataRowView drv = e.Item.DataItem as DataRowView;


int selval = (int)drv["selectvalue"];

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");

RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");


}
====================

DataRowView is just somewhat like DataRow, you can access those database
column's value through index or name:

#DataRowView Class
http://msdn2.microsoft.com/en-us/library/system.data.datarowview(VS.80).aspx

Then, you can set certain RadioButton's Checked property to false or true
based on the column's value.

Hope this helps. If you still have anything unclear, please feel free to
post here.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no
rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Thanks for your response Vikas,

So have you tried narrow down the Null reference exception to see which
object is the cause of the Null refrence exception. I guess it's the
certain column you try retrieving through the DataRowView... You can put
some code to check whether the certain column is null, like:

drv["columnname"] == null

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
V

Vikas Kumar

Thanks for reply Sir
I tried like this now
DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");

RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");

if(drv["Permission"] == null)

{

Response.Write("hell");

}



it gives error on line-if(drv["Permission"] == null

i checked it putting break pts it gives error on this if statement only

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 80: RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");
Line 81: RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");
Line 82: if(drv["Permission"] == null)
Line 83: {
Line 84: Response.Write("hell");

Source File: d:\ankit\property\user\manageuser.aspx.cs Line: 82

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
property.user.manageuser.listUser_ItemDataBound(Object sender,
DataListItemEventArgs e) in d:\ankit\property\user\manageuser.aspx.cs:82
System.Web.UI.WebControls.DataList.OnItemDataBound(DataListItemEventArgs
e)
System.Web.UI.WebControls.DataList.CreateItem(Int32 itemIndex,
ListItemType itemType, Boolean dataBind, Object dataItem)
System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean
useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
property.user.manageuser.Page_Load(Object sender, EventArgs e) in
d:\ankit\property\user\manageuser.aspx.cs:49
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()



Steven Cheng said:
Thanks for your response Vikas,

So have you tried narrow down the Null reference exception to see which
object is the cause of the Null refrence exception. I guess it's the
certain column you try retrieving through the DataRowView... You can put
some code to check whether the certain column is null, like:

drv["columnname"] == null

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no
rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Thanks for your followup Vikas,

It seems that the error is caused by the DataRowView object(drv), what's
the datasource you used to bind the dataList? Using DataSet/DataTable or
SqlDataSource control? I'm thinking that the datasource object is not
DataView, thus, the DataItem in the "ItemDataBound" event should not be
"DataRowView". You can printout the data object's type through the
following code:

Response.Write(e.Item.DataItem.GetType().ToString());

Please feel free to let me know if you got any other finding.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
V

Vikas Kumar

it gives error again

i am binding data in datasource like this through datareader and i have no
problem in binding through any other means eg: dataset if it will work

string strsql="select UID,FName,Permission,Email1 from Userinfo where
ParentID="+ userid;

Database db=DatabaseFactory.CreateDatabase();

DBCommandWrapper cmd=db.GetSqlStringCommandWrapper(strsql);

dr = db.ExecuteReader(cmd);

listUser.DataSource=dr;

listUser.DataBind();

dr.Close();

protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

Response.Write(e.Item.DataItem.GetType().ToString())

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 75: protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
Line 76: {
Line 77: Response.Write(e.Item.DataItem.GetType().ToString());
Line 78:
Line 79: DataRowView drv = e.Item.DataItem as DataRowView
 
S

Steven Cheng[MSFT]

Thanks for your quick response Vikas,

Quite strange. For Datareader, the data object bound to each DataList Row
should be a System.Data.Common.DbDataRecord object. Then, try directly
checking the e.Item.DataItem to see whether it is null.

Also, comment out other code to isolate the problem.(not to involve othe
potential null reference exception).

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
V

Vikas Kumar

protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

if(e.Item.DataItem==null)

{

Response.Write("its null");

}



checked like this it enters the if statement to respose.write("its null")
 
S

Steven Cheng[MSFT]

Strange behavior, seems the datasource has some problem. Are you sure the
Datareader contains record in it? Based on my local test, I can correct get
the DataItem in ItemdataBound event when binding to a DataReader.

If convenient, would you provide a simplified test page so that I can have
a look and test on my side? I'm thinking this is a purely project or
machine specific issue.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
V

Vikas Kumar

public class manageuser : System.Web.UI.Page
{ public IDataReader dr;

protected System.Web.UI.WebControls.DataList listUser;

private void Page_Load(object sender, System.EventArgs e)

int userid = int.Parse(Request.Cookies["UIDCookie"].Value.ToString());
string strsql="select UID,FName,Permission,Email1 from Userinfo where
ParentID="+ userid;
Database db=DatabaseFactory.CreateDatabase();
DBCommandWrapper cmd=db.GetSqlStringCommandWrapper(strsql);

dr = db.ExecuteReader(cmd);
listUser.DataSource=dr;
listUser.DataBind();
dr.Close();
}



protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
if(e.Item.DataItem==null)
{
Response.Write("its null");
}

DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");
RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");
RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");


Response.Write(drv["Permission"].ToString());

//int setperm = (int)drv["Permission"];




}
}

front end code
<asp:datalist id="listUser" Runat="server" RepeatDirection="Vertical"
Width="100%">
<HeaderTemplate>
<table width="100%">
<tr width="100%">
<td width="25%">Subuser</td>
<td width="25%%">Password</td>
<td width="45%">Posting&nbsp;&nbsp;
Subscription&nbsp;&nbsp; Both</td>
<td width="5%">Edit</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<TABLE width="100%">
<TR width="100%">
<TD
width="25%"><%#DataBinder.Eval(Container.DataItem,"FName")%></TD>
<TD width="25%"><INPUT type=text
value='<%#DataBinder.Eval(Container.DataItem,"Email1")%>' name=txtPass></TD>
<TD align="left" width="45%">
<asp:RadioButton id="rb1" runat="server"
GroupName="gp1"></asp:RadioButton>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:RadioButton id="rb2" runat="server"
GroupName="gp1"></asp:RadioButton>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:RadioButton id="rb3" Runat="server"
GroupName="gp1"></asp:RadioButton></TD>
<TD align="left" width="5%">
<asp:Button id="EditSubuser" Runat="server"
Text="Edit"></asp:Button></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:datalist>

this much i am sure i am getting value from database
bcz if don't write
protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
anything in this function it fills my datalist( 5 rows selected from
database
 
S

Steven Cheng[MSFT]

Hi Vikas,

Thanks for the response.

I think you should remove the following code in the ItemDataBound handler:

=========
DataRowView drv = e.Item.DataItem as DataRowView;


Response.Write(drv["Permission"].ToString());
============


Also, based on my local test, "Null reference exception" is likely caused
by some explicit cast operation. I think you can consider use the
DataBinder.Eval to query property/column value from the DataItem, and use
System.Convert class to perform converting on the object got from Eval, e.g:

=====================
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs
e)
{
Response.Write("<br/>" + e.Item.DataItem);

object obj = DataBinder.Eval(e.Item.DataItem, "id");

Response.Write("<br/>id: " + Convert.ToInt64(obj));


}
=======================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
V

Vikas Kumar

thanks to u very much
solved like this
protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

if(e.Item.DataItem!=null)

{

object obj = DataBinder.Eval(e.Item.DataItem, "Permission");

int x=Convert.ToInt32(obj);


RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");

RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");

if(x==1)

{

rb1.Checked=true;

}

else if(x==2)

{

rb2.Checked=true;

}

else if(x==3)

{

rb3.Checked=true;

}


}




}
 
S

Steven Cheng[MSFT]

Hi Vikas,

Your further code suddenly remind me of one issue. I'm sorry that I should
have noticed this issue earlier since this is a common mistake we would
make(actually I also made it in my test code:( ). I get the cause of the
null reference exception you're encountering, it is because we didn't check
the ItemType in the "ItemDataBound" event, actually, only for "Item" or
"AlternateItem" will the DataItem be available, for some other item such as
Pager, Footer.... , the property is not used. So we should change the code
like below:

protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
...................
//we can access the e.Item.DataItem

}

}

Hope this help clarify the problem further. Sorry for my mistake at the
start.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top