I am using the Gridview control without a DataSource control.
When I click on the Edit button, the page posts back and enters the following code:
<code>
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditingTry
Dim row As GridViewRow = GridView1.Rows(e.NewEditIndex)
Dim ddlCustomerID As DropDownList = row.FindControl("ddlCustomerID")
ddlCustomerID.DataSource = dstCustomers
ddlCustomerID.SelectedIndex = ddlCustomerID.Items.IndexOf(ddlCustomerID.Items.FindByValue(dstCustomers.Tables(0).Rows(row.RowIndex).Item("CustomerID")))
ddlCustomerID.DataBind()
Catch ex As Exception
Response.Write(ex.Message)
End TryEnd Sub
</code>
I cannot get an instance of the control in the above boldfaced code after passing thru that line during debugging. It has a value of Nothing.
My html code is set up as follows (shortened for brevity) within the Gridview control:
<code>
<asp:TemplateField HeaderText="CustomerID">
<EditItemTemplate>
<asp:DropDownList ID="ddlCustomerID" runat="server" Text='<%# Bind("CustomerID") %>' DataValueField="CustomerID" DataTextField="ContactName"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCustomerID" runat="server" Text='<%# Bind("CustomerID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField></code>
As you can see above, I have the name of the control defined in the above code in the EditItemTemplate. I am assuming that during the RowEditing event, I should be able to access the control in order to make it into a dropdownlist. During the RowUpdating event which is fired immediately after the RowEditing, I will get the SelectedValue. During non-editing, it is simply a Label control and grabs the value in the database for the CustomerID. It is only when I want to edit the row, that I cannot get an instance of the control.
Can someone help me out here please? How can I get an instance of the control?
Thanks,
Bill Yeager MCP.Net, BCIP
![]() |
0 |
![]() |
sounds like you have an event order issue. what happens if you look for lblCustomerID in your rowediting event? do you find it?
Peter Kellner
http://73rdstreet.com and blogging at
http://PeterKellner.net
MVP, ASP.NET
![]() |
0 |
![]() |
I am no good with VB.NET but the C# code equaivalent will be the following:
DropDownList ddl = row.FindControl("ddlCustomers") as DropDownList;
The VB.NET might be:
Dim ddlCustomerID As DropDownList = CType(row.FindControl("ddlCustomerID"), DropDownList)
HighOnCoding
Wanna get high!
![]() |
0 |
![]() |
Peter, I do find the lblCustomerID object (which is inside the ItemTemplate of the Template field) in my rowediting event...
How can I get access to the ddlCustomerID object (which is inside the EditItemTemplate of the Template field)? I tried the same logic in the RowCommand event (when the CommandName = "Edit") and got the same results.
I looked up the RowEditing event just to make sure I had the same impression of what it supposed to do which is the following:
"The RowEditing event is raised when a row's Edit button is clicked, but before the GridView control enters edit mode."
This is exactly when I want to have the dropdownlist built, but it's obviosuly not working since I can't get a reference to the ddlCustomerID. If not the RowEditing event, then what event? I checked into the other events and nothing else seems to fit. I can't do it in the RowUpdating event, because at this time the user should have already selected the selectedvalue of the dropdownlist.
I'm frustrated with this one...
Thanks,
Bill Yeager MCP.Net, BCIP
![]() |
0 |
![]() |
Azam, thanks for the reply, but I tried that and still can't get an instance of the dropdownlist.
See my post to Peter...
Thanks,
Bill Yeager MCP.Net, BCIP
![]() |
0 |
![]() |
I thought I had resolved it by placing the following code inside the RowDataBound event:
<code>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBoundTry
If (e.Row.RowType = DataControlRowType.DataRow) AndAlso (e.Row.RowState = DataControlRowState.Edit) Then
Dim ddlCustomerID As DropDownList = DirectCast(e.Row.FindControl("ddlCustomerID"), DropDownList)
ddlCustomerID.DataSource = dstCustomers
ddlCustomerID.SelectedIndex = ddlCustomerID.Items.IndexOf(ddlCustomerID.Items.FindByValue(dstCustomers.Tables(0).Rows(e.Row.RowIndex).Item("CustomerID")))
ddlCustomerID.DataBind()
End If
Catch ex As Exception
Response.Write(ex.Message)
End TryEnd Sub
</code>
Notice the boldfaced line above. I figured on each postback, this event will get fired and enter the above code when the Edit button is pressed. According to the description of the event ("The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control."), this seemed like the place to finally get it done. I was wrong...
This event only gets fired when you go into the page for the first time. In other words, during postbacks, it doesn't get fired (as I was debugging and had a breakpoint set at the "Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound" line of the event).
How is a developer then still supposed to get an instance of a dropdownlist control in order to display it?
Thanks,
Bill Yeager MCP.Net, BCIP
![]() |
0 |
![]() |
I'm stumped at the moment, but need to run out for several hours. I'm sure you could solve the problem by making a simple custom control that inherits from the dropdown list and use that instead of th standard dropdown list. You could pass a bound field (eval(.)) to it and do what you want. there has to be an easier way though. I'm glad the gridviewguy is on this thread. I bet he thinks of something before I get home!
-Peter (anxiously awaiting the answer when I get home)
Peter Kellner
http://73rdstreet.com and blogging at
http://PeterKellner.net
MVP, ASP.NET
![]() |
0 |
![]() |
One last thought before I head out. I'm wondering (need to look in reflector and study events) that what might be happening is that the control does not switch into edit mode until the events are processed which means after page_load nad before page_prerender. this would mean that the rowcreated event or rowdatabound events don't act on the edit row. If this is the case, then you might need to put code in the page_prerender that finds your selected row, then finds the dropdownlist based on that. Just a thought. I don't like it a lot but I'm sure Azam will come to the rescue on this one.
Peter Kellner
http://73rdstreet.com and blogging at
http://PeterKellner.net
MVP, ASP.NET
![]() |
0 |
![]() |
I put together some code for you that you can check it out.
Here is the HTML part of the code:
<asp:GridView ID="gvCategories" runat="Server" AutoGenerateColumns="False" OnRowEditing="gvCategories_RowEditing" OnRowUpdating="gvCategories_RowUpdating">
<Columns>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label id="lblCategoryID" runat="Server" Text = '<%# Eval("CategoryID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category Name">
<ItemTemplate>
<asp:Label id="lblCategoryName" runat="Server" Text = '<%# Eval("CategoryName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlCategoryName" DataSource='<%# GetDataSet() %>' DataTextField="CategoryName" DataValueField="CategoryID" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
Here is the C# code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
gvCategories.DataSource = ds;
gvCategories.DataBind();
}
protected DataSet GetDataSet()
{
string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
protected void gvCategories_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCategories.EditIndex = e.NewEditIndex;
BindData();
}
protected void gvCategories_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// get the value from the dropdownlist
GridViewRow row = gvCategories.Rows[e.RowIndex];
int categoryID = Convert.ToInt32((row.FindControl("ddlCategoryName") as DropDownList).SelectedValue);
}
HighOnCoding
Wanna get high!
![]() |
0 |
![]() |
Azam, your post pointed me in the right direction. I finally got it to work using the following html code:
<code>
<asp:TemplateField HeaderText="CustomerID">
<EditItemTemplate>
<asp:DropDownList ID="ddlCustomerID" runat="server" Text='<%# Bind("CustomerID") %>' DataSource='<%# GetCustomers() %>' DataValueField="CustomerID" DataTextField="ContactName" SelectedValue='<%# Bind("CustomerID") %>' AppendDataBoundItems="true"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCustomerID" runat="server" Text='<%# Bind("CustomerID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField></code>
Notice the boldfaced item above which gives me the selected value of the ddl when edited. There was no code in the code behind to take care of this. I handled the update in the RowUpdating event by using the following code:
<code>
dtblOrders.Rows(0).Item("CustomerID") = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlCustomerID"), DropDownList).SelectedValue
</code>
After further review of this situation, it appears that the RowEditing, RowCommand and RowDataBound events are only useful for the object inside the ItemTemplate and not the object inside the EditItemTemplate. Please let me know if this stands to reason.
The RowCommand event can obviously be also used to tell which command was selected...
Thanks,
Bill Yeager MCP.Net, BCIP
![]() |
0 |
![]() |
I have similar problem to what Azam's described and SelectedValue property was the first option I had on my mind, but when I tried it, it failed. Build finished successfully, but the containing page resulted in invalid use of SelectedValue property which was not contained in the property list. This property is not contained in Intellisense as well. Any idea what I'm missing? Thanks for any help that would point me in the right direction!
Ladislav P. Valik
Microsoft Certified Professional (WebForms, WinForms)
![]() |
0 |
![]() |