Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist in gridview edit mode

Status
Not open for further replies.

NHW

Programmer
Mar 16, 2004
24
0
0
HK
Hi. I'm trying to create a dropdownlist show up when a row is being edited. And the number of items in the dropdownlist depends on the number of rows in the gridview. When i run the code, it's always complaining not being able to reference to the dropdownlist object. Here's the snippet:

Gridview code in the aspx page:
****
<asp:GridView ID="gdvRoles" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="RoleID" DataSourceID="sdsRoles"
OnRowDataBound="gdvContactRoles_RowDataBound">
****

Templatefield in aspx page:
****
<asp:TemplateField HeaderText="Priority" SortExpression="Priority">
<ItemTemplate>
<asp:Label ID="lblRank" runat="server" Text='<%# Eval("rank") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlRank" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
****

RowDataBound event in aspx.vb page:
****
Protected Sub gdvRoles_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdvRoles.RowDataBound
Dim i, intCount As Integer

intCount = gdvRoles.Rows.Count

Dim ddlMakeRank As Control
ddlMakeRank = CType(e.Row.Cells(5).FindControl("ddlRank"), DropDownList)
For i = 1 To intCount
ddlMakeRank.Items.Add(New ListItem(i, i))
Next

End Sub
****

Thanks in advance!

NHW
 
RowDataBound event fires for all rows: headers, spacers, footers, rows, alternate rows. in this event first check to ensure the row is a data row (alternate row is a subclass of data row) if it's not exit. next check if the row is in edit mode. if not exit. then find the control. if the control is null exit. it would look something like this is c#
Code:
protected voidgdvRoles_RowDataBound(object sender, GridViewRowEventArgs e)
{
  var row = e.Row;
  if(row.RowType != GridViewRowType.DataRow) return;
  if(row.RowState != GridViewRowState.Edit) return;

  var ddl = row.FindControl("id of ddl") as DropDownList;
  if(ddl == null) return;

  ddl.DataSource = the options;
  ddl.DataBind();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks a lot for the tip. It's capturing the event correctly now. But I've now run into another problem. When I run a loop to populate the dropdownlist, it seems to be repeating.

Example Code:
***
'After verifying edit mode and retrieved the control ddlRank
For i = 1 To 3
ddlRank.Items.Add(New ListItem(i))
Next
***

And the dropdownlist box will look like this:
1
2
3
1
2
3


Seems like the event was called twice when I click "edit" in the gridview?

NHW
 
change
Code:
for(var i = 1; i<=3;i++)
{
   ddl.Items.Add(new ListItem(i.ToString());
}
to
Code:
ddl.DataSource = new []{1,2,3};
ddl.DataBind();

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks both.

I changed to the .DataSource and DataBind() method and it's working fine now. But I also tried a debug/step-in as advised by Mark and did notice the event was indeed ran consecutively. So why is it that the second case work but not the first?

NHW
 
it could be the handler is wired to the event twice. I have also heard of Casini (VS web server) doubling request processing calls. one way to confirm this is to use IIS instead of Casini for the web server. to change this:
1. right click the web project in the solution explorer pane
2. select properties
3. select the Web tab
4. configure the project to use IIS instead of VS Developer Server

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks again. I shall investigate when I get hold of VS later (I'm using Visual Web Developer at the moment).

Now if I bind the ddl as suggested, how can I pass the changed value back to the GridView's underlying datasource for updates? Right now when I click "Update" in the GridView all the fields get updated except this ddl.

Please pardon the questions as I'm very green to .Net

Thanks again.

NHW
 
[tt]datasource[/tt] is the problem. these "ui" controls are very attractive for beginners because the system will write the code for you. Ultimately they cause more problems than anything else. A good rule of thumb is the more code you write the more control you have over the application.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
On second thoughts I'm running it off directly from an IIS web server (instead of locally). So I guess there must be a bug in the event wiring mechanism?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top