Re: How to Reference Gridview child controls
- From: Mike <Mike@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 18 Mar 2008 18:40:00 -0700
Hi, I'm sorry, I don't know C#...but the code I posted does work for me in
Visual Basic. I have heard there is a website that will convert visual basic
to C#; maybe google it? good luck!
"sskandekar@xxxxxxxxx" wrote:
On Feb 21, 3:46 am, Mike <M...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:.
OK OK for what it's worth, here is the result of my stupid 25 hours of
digging...sheesh!
This does not throw an error:
Dim ddlVendorFilter As DropDownList =
gvCourses.HeaderRow.FindControl("ddlVendor")
I have not tried including it in a Ctype expression, and I'm not sure it's
necessary...
The key is including the HeaderRow in the code, because the control is in
the headerRow...I guess this is "duh!" for many of you, but perhaps it may
help a fool like me save several days of frustration...good luck!
"Mike" wrote:
A little more update, I've also tried:
Dim ddlVendorFilter As DropDownList =
CType(gvCourses.FindControl("ddlVendor"), DropDownList)
MsgBox("ddlVendor Selected Value is" &
ddlVendorFilter.SelectedValue) 'Returns error
"Mike" wrote:
Hello,
I have a gridview that I want to basically convert to behavior like Excel
autofilter. I have created a dropdown list and put it into the header of the
gridview, but I can't seem to reference it correctly. I have been trolling
through the net and ms references for hours but I can't get it straight.
Here is the html code:
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="datagridtest4.aspx.vb" Inherits="datagridtest4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1">
<Columns>
<asp:TemplateField HeaderText="Vendor" SortExpression="Vendor">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("Vendor") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True"
DataSourceID="AccessDataSource1"
DataTextField="Vendor"
DataValueField="VendorID"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1"
runat="server"
DataFile="~/App_Data/courses.mdb"
SelectCommand="SELECT * FROM [tblVendors] ORDER BY
[Vendor]">
</asp:AccessDataSource>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("Vendor") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="BasicCourseName"
HeaderText="BasicCourseName"
SortExpression="BasicCourseName" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/courses.mdb"
SelectCommand="SELECT * FROM
[qryCoursesByVendors]"></asp:AccessDataSource>
</form>
</body>
</html>
I don't know where to attach the code behind the form; on the dropdown list
changed event, or the page load event, or...? I am sorry if this is stupid,
but I can't seem to get it. Here is what I have in the code behind the form:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Dim ddlVendor As DropDownList
ddlVendor = Me.GridView1.FindControl("DropDownList1")
AccessDataSource1.FilterExpression = "VendorID = " &
ddlVendor.SelectedItem.Text
End Sub
I have also experimented with:
ddlVendor = CType(GridView1.TemplateControl.FindControl("DropDownList1"),
DropDownList)
I get the following error message:
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 36: Dim ddlVendor As DropDownList
Line 37: ddlVendor = Me.GridView1.FindControl("DropDownList1")
Line 38: AccessDataSource1.FilterExpression = "VendorID = " &
ddlVendor.SelectedItem.Text
Line 39: End If
Is there any way to implement this type of thing, or is this a nightmare? I
am a beginning programmer, and I've already spent many days making only
incremental advances.
thanks in advance for your help and support.
mike
hi ,
I am also doing the same thing in C#.net
i had the error at the line
DropDownList ddl1 = GridView1.HeaderRow.FindControl("ddl");
the error is
Cannot implicitly convert type 'System.Web.UI.Control' to
'System.Web.UI.WebControls.DropDownList'. An explicit conversion
exists (are you missing a cast?)
Please reply me
Sagar Kandekar
- References:
- Re: How to Reference Gridview child controls
- From: sskandekar
- Re: How to Reference Gridview child controls
- Prev by Date: Re: Custom Control, ITemplate and nested bound controls
- Next by Date: GridView control adapter issue
- Previous by thread: Re: How to Reference Gridview child controls
- Next by thread: GridView control adapter issue
- Index(es):
Relevant Pages
|