473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problems reading controls' properties when contained in a TemplatedColumn

I'm trying to use the DataGrid's editing with a DropDownList. I've
tried using every code sample I've seen and none of them seem to be
able to solve my problem.

When I call e.Item.FindControl("name-of-my-DropDownList"), where e is a
DataGridCommandEventArgs passed to the UpdateCommand of the DataGrid, I
get an empty DropDownList. In fact, when I call
e.Item.FindControl("name-of-a-label") in the non-editable portion of my
template, I get an empty label.

ASP.Net doesn't have a problem with my accessing the DropDownList from
methods that it calls, interestingly enough. I can see the contents of
the DropDownList just fine when it calls its PreRender method and I
access the method's sender.

This is really weird...can someone lend me a hand here?

I'd really appreciate it.

-Starwiz

Here's my datagrid code (with appologies in advance to anyone whose
newsreader doesn't format this nicely):

<asp:datagrid id="DataGrid" datakeyfield="ID" (and other parameters)>
<columns>
<asp:templatecolumn headertext="Status">
<itemtemplate>
<asp:label id="lblStatus" enableviewstate="True"
runat="server" text='<%#
((System.Data.DataRowView)Container.DataItem)[StatusField] %>'>
</asp:label>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist autopostback="False"
enableviewstate="True" runat="server" id="ddlStatus"
onprerender="SetStatusIndex" />
</edititemtemplate>
</asp:templatecolumn>
<asp:editcommandcolumn buttontype="LinkButton"
updatetext="Update" canceltext="Cancel"
edittext="Edit"></asp:editcommandcolumn>
<!--The rest of the columns are auto-populated-->
</columns>
</asp:datagrid>
*********************
And my C# code:

private bool bindDataGrid = false;
private void ViewAll_PreRender(object sender, System.EventArgs e)
{
if(bindDataGrid)
RefreshDataGrid();
}

private void RefreshDataGrid()
{
//...this code doesn't matter much
//but let me know if you want to see it.
}

protected string SelectedStatus = null;
private void DataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SelectedStatus = ((Label)e.Item.FindControl("lblStatus")).Text;
//SelectedStatus is always an empty string.

DataGrid.EditItemIndex = e.Item.ItemIndex;
bindDataGrid = true;
}

protected void SetStatusIndex(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
DataTable dt = DBAccessor.GetPatientStatuses();
foreach(DataRow dr in dt.Rows)
ddl.Items.Add(new ListItem((string)dr["Status"],
((int)dr["StatusID"]).ToString()));

ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByValue(SelectedSt atus));
}

private void DataGrid_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int index = e.Item.DataSetIndex;
DataTable dt = (DataTable)Session[PatientDT];
int id = (int)dt.Rows[index][IDField];
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatus");

int newStatus = int.Parse(ddl.SelectedValue);
//ddl.SelectedValue is always an empty string.
//In fact, ddl.Items is empty, too;
//there aren't any meaningful properties
//of the ddl that I can read at all.

DBAccessor.SetPatientStatus(id, newStatus);
DataGrid.EditItemIndex = -1;
bindDataGrid = true;
}

//Yes, this is messy. It was a failed attempt
//at fixing this problem that I just haven't gotten
//rid of yet...
private void DataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatus");
if(ddl != null)
SetStatusIndex(ddl, null);
}
else if(e.Item.ItemType == ListItemType.Item)
{
try
{
Label lbl = (Label)e.Item.FindControl("lblStatus");
lbl.Text =
(string)((System.Data.DataRowView)e.Item.DataItem)[StatusField];
}
finally {}
}
}

Nov 18 '05 #1
4 1614
I'm guessing that you're using a datasource to fill the dropdownlist, that
you're binding the datasource to the ddl *each* time the page loads, but only
filling the datasource *once* when the page first loads. Is that possible?

The reason you'll see things in Prerender and then not again is that the
events go: Page Load --> Process Events --> Page Prerender, so it's still
there at Prerender but is only still there on the next pass for Process
Events if you've put it back at page load (or it's stored in viewstate and
the framework has put it back for you).

If this doesn't do anything for you then post us a few snippets of what's
happening at Page Load, ItemDataBound, and UpdateCommand.

Bill

"starwiz" wrote:
I'm trying to use the DataGrid's editing with a DropDownList. I've
tried using every code sample I've seen and none of them seem to be
able to solve my problem.

When I call e.Item.FindControl("name-of-my-DropDownList"), where e is a
DataGridCommandEventArgs passed to the UpdateCommand of the DataGrid, I
get an empty DropDownList. In fact, when I call
e.Item.FindControl("name-of-a-label") in the non-editable portion of my
template, I get an empty label.

ASP.Net doesn't have a problem with my accessing the DropDownList from
methods that it calls, interestingly enough. I can see the contents of
the DropDownList just fine when it calls its PreRender method and I
access the method's sender.

This is really weird...can someone lend me a hand here?

I'd really appreciate it.

-Starwiz

Here's my datagrid code (with appologies in advance to anyone whose
newsreader doesn't format this nicely):

<asp:datagrid id="DataGrid" datakeyfield="ID" (and other parameters)>
<columns>
<asp:templatecolumn headertext="Status">
<itemtemplate>
<asp:label id="lblStatus" enableviewstate="True"
runat="server" text='<%#
((System.Data.DataRowView)Container.DataItem)[StatusField] %>'>
</asp:label>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist autopostback="False"
enableviewstate="True" runat="server" id="ddlStatus"
onprerender="SetStatusIndex" />
</edititemtemplate>
</asp:templatecolumn>
<asp:editcommandcolumn buttontype="LinkButton"
updatetext="Update" canceltext="Cancel"
edittext="Edit"></asp:editcommandcolumn>
<!--The rest of the columns are auto-populated-->
</columns>
</asp:datagrid>
*********************
And my C# code:

private bool bindDataGrid = false;
private void ViewAll_PreRender(object sender, System.EventArgs e)
{
if(bindDataGrid)
RefreshDataGrid();
}

private void RefreshDataGrid()
{
//...this code doesn't matter much
//but let me know if you want to see it.
}

protected string SelectedStatus = null;
private void DataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SelectedStatus = ((Label)e.Item.FindControl("lblStatus")).Text;
//SelectedStatus is always an empty string.

DataGrid.EditItemIndex = e.Item.ItemIndex;
bindDataGrid = true;
}

protected void SetStatusIndex(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
DataTable dt = DBAccessor.GetPatientStatuses();
foreach(DataRow dr in dt.Rows)
ddl.Items.Add(new ListItem((string)dr["Status"],
((int)dr["StatusID"]).ToString()));

ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByValue(SelectedSt atus));
}

private void DataGrid_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int index = e.Item.DataSetIndex;
DataTable dt = (DataTable)Session[PatientDT];
int id = (int)dt.Rows[index][IDField];
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatus");

int newStatus = int.Parse(ddl.SelectedValue);
//ddl.SelectedValue is always an empty string.
//In fact, ddl.Items is empty, too;
//there aren't any meaningful properties
//of the ddl that I can read at all.

DBAccessor.SetPatientStatus(id, newStatus);
DataGrid.EditItemIndex = -1;
bindDataGrid = true;
}

//Yes, this is messy. It was a failed attempt
//at fixing this problem that I just haven't gotten
//rid of yet...
private void DataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatus");
if(ddl != null)
SetStatusIndex(ddl, null);
}
else if(e.Item.ItemType == ListItemType.Item)
{
try
{
Label lbl = (Label)e.Item.FindControl("lblStatus");
lbl.Text =
(string)((System.Data.DataRowView)e.Item.DataItem)[StatusField];
}
finally {}
}
}

Nov 18 '05 #2
Thanks for the quick reply, Bill.

The SetStatusIndex method fills the dropdownlist. It's called only by
the DataGrid's ItemDataBound event (when it's called for the row
containing the edititemtemplate) see DataGrid_ItemDataBound(). I've
also tried putting that method call in the DropDownList's PreRender
handler to no avail.

Anyway, SetStatusIndex() isn't called when you press the update link; I
just set a breakpoint there and checked it.

I'm not exactly sure what "snippets of what's happening at PageLoad"
etc. means, but I'll give you what I think you might be looking for:
When SetStatusIndex is called, I can see the DropDownList and all its
properties just fine. However, when DataGrid_UpdateCommand and
DataGrid_EditCommand are called, the DropDownList and the Label show no
values for significant properties (like Items, SelectedValue, or Text).
The objects aren't null, but they certainly don't contain the
information I want.

Is that what you were looking for? If it's not, I'm happy to provide
you with whatever information you think would be helpful.
Thanks again,
-Starwiz

Nov 18 '05 #3
Are you rebinding the data to the grid on *each* postback? Also, where are
you getting the data that SetStatusIndex uses to fill the list? Is it
hard-coded, or does it come from a datasource? In the latter case, is it
possible that you're rebinding the data on every postback, but on the passes
after the first you are not reloading the data, and are therefore binding to
an empty datasource (which doesn't cause errors, just gives you an empty
list).

If this doesn't ring any bells, post code snippets for SetStatusIndex,
ItemDataBound, and wherever you're calling .databind on the grid (e.g. Page
Load).

Bill

"starwiz" wrote:
Thanks for the quick reply, Bill.

The SetStatusIndex method fills the dropdownlist. It's called only by
the DataGrid's ItemDataBound event (when it's called for the row
containing the edititemtemplate) see DataGrid_ItemDataBound(). I've
also tried putting that method call in the DropDownList's PreRender
handler to no avail.

Anyway, SetStatusIndex() isn't called when you press the update link; I
just set a breakpoint there and checked it.

I'm not exactly sure what "snippets of what's happening at PageLoad"
etc. means, but I'll give you what I think you might be looking for:
When SetStatusIndex is called, I can see the DropDownList and all its
properties just fine. However, when DataGrid_UpdateCommand and
DataGrid_EditCommand are called, the DropDownList and the Label show no
values for significant properties (like Items, SelectedValue, or Text).
The objects aren't null, but they certainly don't contain the
information I want.

Is that what you were looking for? If it's not, I'm happy to provide
you with whatever information you think would be helpful.
Thanks again,
-Starwiz

Nov 18 '05 #4
I figured this one out a while ago, but I forgot to post my solution
here for posterity.

The problem was that I was using a LinkButton to submit the DataGrid's
updates. Since ASP.Net (and any other online application) is really
just server-generated HTML, the LinkButton is implemented by a
javascript call which refreshes the page. Because links cannot submit
forms, the changes to the drop down list were lost.

So there are two simple solutions: Either make the drop down list
automatically postback every time something in it changes (kind of
annoying, but works), or use regular buttons, rather than linkbuttons.

Hope this helps someone somehow,
-Starwiz

Bill Borg wrote:
Are you rebinding the data to the grid on *each* postback? Also, where are you getting the data that SetStatusIndex uses to fill the list? Is it hard-coded, or does it come from a datasource? In the latter case, is it possible that you're rebinding the data on every postback, but on the passes after the first you are not reloading the data, and are therefore binding to an empty datasource (which doesn't cause errors, just gives you an empty list).

If this doesn't ring any bells, post code snippets for SetStatusIndex, ItemDataBound, and wherever you're calling .databind on the grid (e.g. Page Load).

Bill

"starwiz" wrote:
Thanks for the quick reply, Bill.

The SetStatusIndex method fills the dropdownlist. It's called only by the DataGrid's ItemDataBound event (when it's called for the row
containing the edititemtemplate) see DataGrid_ItemDataBound(). I've also tried putting that method call in the DropDownList's PreRender
handler to no avail.

Anyway, SetStatusIndex() isn't called when you press the update link; I just set a breakpoint there and checked it.

I'm not exactly sure what "snippets of what's happening at PageLoad" etc. means, but I'll give you what I think you might be looking for: When SetStatusIndex is called, I can see the DropDownList and all its properties just fine. However, when DataGrid_UpdateCommand and
DataGrid_EditCommand are called, the DropDownList and the Label show no values for significant properties (like Items, SelectedValue, or Text). The objects aren't null, but they certainly don't contain the
information I want.

Is that what you were looking for? If it's not, I'm happy to provide you with whatever information you think would be helpful.
Thanks again,
-Starwiz


Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: William Bradley | last post by:
First of all I have been working with Access 97 and this morning the owner of the business phoned me to inform me that he had updated to Access 2000 and parts of my forms would not work anymore. ...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
1
by: Earl Teigrob | last post by:
Background: When I create a ASP.NET control (User or custom), it often requires security to be set for certain functionality with the control. For example, a news release user control that is...
1
by: Sky Sigal | last post by:
(PS: Cross post from microsoft.pulic.dotnet.framework.aspnet.webcontrols) I've been looking lately for a way to keep the Properties panel for Controls 'clean'... My goal is to keep similar...
3
by: Jim Hubbard | last post by:
If I create a usercontrol that is a tab with a webbrowser control on it, by default the new control exposes no properties of the tab or webbrowser control. What is the easiest way to bubble the...
8
by: Rohit Kumbhar | last post by:
Hi, I am writing a Windows Forms C# application for reading files on the disk. My problem is the UI freezes when restored after being minimized for a long time . I have a separate thread...
3
by: =?Utf-8?B?R3JlZw==?= | last post by:
I am used to using third party controls when it comes to setting up appearences. But, now I am using Visual Basic.Net controls that come standard with the product. I've come across a frustration...
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.