GridView and Hidden Fields - asp.net

I've got a Web app, in VB/ASPX, with a GridView fill with a SqlDataSource in my aspx file. Like this :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" SkinID="dataGrid" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField AccessibleHeaderText="id_session" HeaderText="id_session">
<EditItemTemplate>
<asp:TextBox ID="txt_id_session" runat="server" Text='<%# Bind("id_session") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_id_session" runat="server" Text='<%# Bind("id_session") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Formation_2014ConnectionString %>"
SelectCommand="SELECT s.id_session, f.libelle_formation, s.date_debut_session, s.date_fin_session, COUNT(p.id_personne) AS Expr1 FROM Sessions AS s LEFT OUTER JOIN Participe AS p ON p.id_session = s.id_session AND p.actif = 1 RIGHT OUTER JOIN Formation AS f ON f.id_formation = s.id_formation WHERE (s.date_fin_session > GETDATE()) OR (s.date_fin_session < GETDATE()) OR (S.date_fin_session = GETDATE()) GROUP BY s.id_session, f.libelle_formation, s.date_debut_session, s.date_fin_session" >
</asp:SqlDataSource>
This parts works, after that I'm hiding the first column, "id_session" with this code in my CodeBehind :
Protected Sub OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowCreated
GridView1.Columns(0).Visible = False
'e.Row.Cells(0).Visible = False 'This way delete my paging
(RequĂȘte)
End Sub
And after that, I have to get the value of this Hidden column in my vb code, i'm trying different way but nothing ><
Dim id_session = GridView1.SelectedRow.Cells(0).Controls(0).ToString 'Return System.Web.UI.Literalcontrol
Dim id_session = GridView1.SelectedRow.RowIndex 'Return number of line
Sorry for my english, i'm French !

use the Hiddenfield
try the following code insted of useing textbox in the firest column
<asp:HiddenField ID="hf_sessionId" runat="server" Value='<%# Bind("id_session") %>' />
it will not show to the UI and you can easily access the value from it
with this code you don't need the RowCreated event of the grid
dear remove this code
<ItemTemplate>
<asp:Label ID="lbl_id_session" runat="server" Text='<%# Bind("id_session") %>'></asp:Label>
</ItemTemplate>
and put the hiddenfield into the other itemtemplate which you want to display

Instead of "GridView1.Columns(0).Visible = False" change your code like this,
GridView1.Columns(0).style("display")="none"

I finally found this method :
Dim id_session = (CType(GridView1.SelectedRow.Cells(0).Controls(1), Label)).Text
It Work !

Related

How to code a button click event in a GridView that returns a value from its row in the GridView

Apologies for the slightly convoluted title.
Basically, I have a project where I have a database connected to it. I'm displaying some of the contents of the database using a GridView, which is all up and running, but the way I'm designing the page I need to be able to click a button in each row that in essence exports the value of one of the cells in the row to a subroutine.
I've tried googling it quite a bit and trying related things I found here, but nothing I could find would function like I would like it to.
The relevant code is below.
Markup:
<asp:GridView ID="Staffgv" runat="server" AutoGenerateColumns="false" OnRowCommand="Staffgv_RowCommand" AllowPaging="true" PageSize="20" OnPageIndexChanging="Staffgv_PageIndexChanging" BackColor="#f9f9f9" CssClass="gvStyle" >
<Columns>
<asp:TemplateField HeaderText="Start" InsertVisible="False" SortExpression="DateTimeStart">
<HeaderStyle Width="70px" CssClass="hdrGvStart"/>
<ItemTemplate>
<asp:Label ID="lblDateTimeStart" runat="server" Text='<%# Bind("DateTimeStart", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Finish" SortExpression="DateTimeEnd">
<HeaderStyle Width="70px" CssClass="hdrGvFinish"/>
<ItemTemplate>
<asp:Label ID="lblDateTimeEnd" runat="server" Text='<%# Bind("DateTimeEnd", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Forename" SortExpression="Forename">
<HeaderStyle Width="140px" CssClass="hdrGvForename"/>
<ItemTemplate>
<asp:Label ID="lblForename" runat="server" Text='<%# Bind("Forename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname" SortExpression="Surname">
<HeaderStyle Width="140px" CssClass="hdrGvSurname"/>
<ItemTemplate>
<asp:Label ID="lblSurname" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle CssClass="gvHeaderEdit" />
<ItemTemplate>
<asp:Button ID="Btnapptid" runat="server" Text="" CssClass="btnGVEdit" CommandName="FillStaffTables" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the relevant VB code:
Private Sub Staffgv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Staffgv.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim row = DirectCast(e.Row.DataItem, DataRowView)
Dim apptid As Integer = Integer.Parse(row("AppointmentID").ToString)
Dim Btnapptid = DirectCast(e.Row.FindControl("Btnapptid"), Button)
'Btnapptid.Text = apptid
Btnapptid.ToolTip = apptid
End Select
End Sub
Protected Sub Staffgv_RowCommand(ByVal sender As Object, e As GridViewCommandEventArgs)
If (e.CommandName = "FillStaffTables") Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
lblTxtTester.Text = "AppointmentID"
TextboxTester.Text = index
End If
End Sub
If anyone would like more code like the code used to fill the GridView I'll post it, just didn't want to post too much irrelevant code.
Thanks
Solution A
When you only want 1 value from the gridview row when the button is clicked.
You could simply utilize the CommandArgument of your button (Btnapptid). This assumes that you don't need the gridview row index for when the button is clicked. If you do need this, then please see Solution B, otherwise continue here.
First, you'd need to modify your button's CommandArgument in the aspx page
ASPX
<asp:Button ID="Btnapptid" runat="server" Text="" CssClass="btnGVEdit"
CommandName="FillStaffTables" CommandArgument='<%# Bind("AppointmentID") %>'/>
Then you should be able to grab the AppointmentID like this
VB (inside Staffgv_RowCommand())
If (e.CommandName = "FillStaffTables") Then
txtAppointmentID.Text = e.CommandArgument
End If
Solution B
When you need more than 1 value from the gridview row when the button is clicked
Please note that this solution requires a couple changes on your end
Create an additional control (in the gridview) which should hold the value that you want to get when the button is clicked.
Fill said control with the value you want (via Bind() in the UI or in RowDataBound in the codebehind).
Next, in your RowCommand you'd be able to grab the newly created control with the help of the index variable (from your example) like so
Staffgv.Rows(index).FindControl("YOUR_CONTROLS_ID")
Example
Say that you decide to create a HiddenField control in which to store the value from your database
ASPX (hidden field should be somewhere inside the gridview - right under Btnapptid should be fine)
<asp:HiddenField ID="hfMyControl" runat="server" Visible="False"
Value='<%# Bind("SOME_DB_COLUMN") %>'/>
VB (inside Staffgv_RowCommand())
Dim hfMyControl As HiddenField = _
DirectCast(Staffgv.Rows(index).FindControl("hfMyControl"), HiddenField)
Then simply use the value of hfMyControl
hfMyControl.Value
You could also repeat all this using multiple controls in order to potentially access multiple DB values stored in controls in the gridview.

FindControl throwing NullReferenceException

Within my ASP gridview, I have the following (updated to show full gridview):
<asp:GridView ID="TPAnnuity_GridView" AllowSorting="true" AllowPaging="true" Runat="server"
DataSourceID="TPAnnuity_SqlDataSource" DataKeyNames="AnnuityTotalPointsID"
AutoGenerateColumns="False" ShowFooter="true" PageSize="20">
<Columns>
<asp:TemplateField HeaderText="Company" SortExpression="CompanyName" HeaderStyle-VerticalAlign="Bottom">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="EditACompanyID" runat="server" DataSource="<%# ddlCompanyDS %>" DataValueField="CompanyID" DataTextField="CompanyName" selectedValue='<%# Bind("CompanyID") %>'></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="NewCompanyID" runat="server" DataSource="<%# ddlCompanyDS %>" DataValueField="CompanyID" DataTextField="CompanyName"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="NewCompanyID" Display="Dynamic" ForeColor="" ErrorMessage="You must enter a value. *" Enabled="false"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<br />
<i>No Commission Data to display.</i>
<br />
<br />
</EmptyDataTemplate>
</asp:GridView>
And within my back end, I have the following:
Sub TPAnnuity_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles TPAnnuity_GridView.RowCommand
If e.CommandName = "Cancel" Then
'Reset Footer Row input fields
CType(TPAnnuity_GridView.FooterRow.FindControl("NewCompanyID"), DropDownList).SelectedIndex = 0
ElseIf e.CommandName = "Insert" Then
TPAnnuity_SqlDataSource.InsertParameters.Clear()
Dim test1 As New Parameter("CompanyIDInt", TypeCode.Int32)
test1.DefaultValue = CType(TPAnnuity_GridView.FooterRow.FindControl("NewCompanyID"), DropDownList).SelectedValue
TPAnnuity_SqlDataSource.InsertParameters.Add(test1)
TPAnnuity_SqlDataSource.Insert()
ElseIf e.CommandName = "Update" Then
TPAnnuity_SqlDataSource.UpdateParameters.Clear()
Dim param1 As New Parameter("CompanyIDInt", TypeCode.Int32)
param1.DefaultValue = CType(TPAnnuity_GridView.FooterRow.FindControl("EditACompanyID"), DropDownList).SelectedValue ****THIS IS THE PROBLEM LINE****
TPAnnuity_SqlDataSource.UpdateParameters.Add(param1)
TPAnnuity_SqlDataSource.Update()
End If
End Sub
The Cancel and Insert functions work just fine, operating off of the footer. Every time hit the "Update" button, I get a NullReferenceException on the param1.Default Value = line.
Can anyone help me figure out what's going on here?
Your row should look like this:
param1.DefaultValue = CType(e.CommandSource.FindControl("EditACompanyID"), DropDownList).SelectedValue
Instead of utilizing the FooterRow, you have to use the source row. Since you passed that in with e, you can use this.

Populating ASP.Net DetailsView textbox with default value when entering Add mode

We would like to populate a textbox with today's date as a default value when the user goes into "Add" mode on an ASP.Net DetailsView from a VB.Net code-behind file. Can you show me how to get it populated?
Here is the markup for the textbox we wish to populate:
<asp:TemplateField HeaderText="Late Time Arrived At School:" SortExpression="LateTimeArrivedAtSchool">
<EditItemTemplate>
<asp:TextBox ID="TextBoxLateTimeArrivedAtSchool" runat="server"
Text='<%# Bind("LateTimeArrivedAtSchool", "{0:hh:mm}") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBoxLateTimeArrivedAtSchool" runat="server"
Text='<%# Bind("LateTimeArrivedAtSchool", "{0:hh:mm}") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelLateTimeArrivedAtSchool" runat="server"
Text='<%# Bind("LateTimeArrivedAtSchool", "{0:hh:mm}") %>'></asp:Label>
</ItemTemplate>
<ItemStyle ForeColor="Blue" />
</asp:TemplateField>
We also have set up OnItemCommand as shown in this markup:
<asp:DetailsView
ID="DetailsView"
runat="server"
AutoGenerateRows="False"
Height="50px"
Width="207px"
DataSourceID="SqlDataSourceDetails"
DataKeyNames="ID"
OnItemCommand="DetailsViewDetails_ItemCommand">
<Fields>
The code-behind file has this coding:
Protected Sub DetailsViewDetails_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs)
Select Case e.CommandName
Case "Add"
Case "Edit"
ButtonAddNewAttendance.Enabled = False
Case "Delete"
Case "Update"
ButtonAddNewAttendance.Enabled = True
Case "Insert"
End Select
End Sub
I tried to type TextBoxLateTimeArrivedAtSchool.Text in the Case -> "Add" but got a message that TextBoxLateTimeArrivedAtSchool was not defined. I'm not sure why intellisence did not find it from the markup.
I was thinking Case -> "Add" was the correct place to populate the textbox but looks like that's not the place to do it.
I think you have to find the control first. The code should be something like this:
Select Case e.CommandName
Case "Add"
Dim txtBox As TextBox
textBox = DetailsViewDetails.FindControl("TextBoxLateTimeArrivedAtSchool")
textBox.Text = DateTime.Now

DetailsView can't figure how to trigger update event

I've been trying to make use of DetailsView for the past few days to show and edit data from a single record taken from a GridView, both read their datafrom an SqlDataSource.
I've been searching the internet and the MSDN for info about how to use a DetailsView to edit data and I managed to cobble something together, the problem is that apparently the actual updating method doesn't run.
So here's an extract of my code
GridView In the page:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DefaultMode="Edit">
<fields>
<asp:TemplateField HeaderText="Codice Cliente">
<ItemTemplate>
<asp:Label ID="lblCliCod" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_cod") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliCod" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_cod")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Descrizione">
<ItemTemplate>
<asp:Label ID="lblCliDesc" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_desc")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliDesc" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_desc")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nome Utente">
<ItemTemplate>
<asp:Label ID="lblCliUser" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_user")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliUser" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_user")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblCliPass" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_pass")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliPass" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_pass")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amministratore">
<ItemTemplate>
<asp:Label ID="lblCliAdmin" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_admin")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkCliAdminE" runat="server" Text='<%# CBool(DataBinder.Eval(Container, "DataItem.cli_admin"))%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton Text="Aggiorna" ID="UpdateButton" runat="Server" CommandName="Update"></asp:LinkButton>
<asp:LinkButton Text="Elimina" ID="DeleteButton" runat="server" CommandName="Edit"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</fields>
</asp:DetailsView>
Relevant parts of the Codebehind:
Private Sub SetupDataSource() 'this is the sqldatasource I'm using for the detailsview
SqlDataSource2.ConnectionString = Assist.connectionString
SqlDataSource2.SelectCommand = detailsSelect
SqlDataSource2.SelectParameters.Add(New Parameter("id"))
SqlDataSource2.UpdateCommand = detailsUpdate
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_cod"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_desc"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_user"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_pass"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_admin"))
End Sub
Private Sub SetupDetailsView(id As Int32)
DetailsView1.AutoGenerateRows = False
DetailsView1.DataSource = SqlDataSource2
SqlDataSource2.SelectParameters("id").DefaultValue = id
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
cn.Open()
SetupDataSource()
If Not IsPostBack Then
Dim id As String = Request.QueryString("id")
If id <> "" Then
SetupDetailsView(id)
DetailsView1.DataBind()
End If
End If
End Sub
Protected Sub DetailsView1_ItemUpdating(sender As Object, e As DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Dim cliCod As String = (CType(DetailsView1.FindControl("txtCliCod"), TextBox)).Text.ToString()
Dim cliDesc As String = (CType(DetailsView1.FindControl("txtCliDesc"), TextBox)).Text.ToString()
Dim cliUser As String = (CType(DetailsView1.FindControl("txtCliUser"), TextBox)).Text.ToString()
Dim cliPass As String = (CType(DetailsView1.FindControl("txtCliPass"), TextBox)).Text.ToString()
Dim cliAdmin As Boolean = If((CType(DetailsView1.FindControl("chkCliAdminE"), CheckBox)).Checked = True, True, False)
SqlDataSource2.UpdateParameters("cli_cod").DefaultValue = cliCod
SqlDataSource2.UpdateParameters("cli_desc").DefaultValue = cliDesc
SqlDataSource2.UpdateParameters("cli_user").DefaultValue = cliUser
SqlDataSource2.UpdateParameters("cli_pass").DefaultValue = cliPass
SqlDataSource2.UpdateParameters("cli_Admin").DefaultValue = If(cliAdmin = True, "1", "0")
DetailsView1.DataBind()
End Sub
I'm either missing something or doing it wrong somewhere, which one is it and why?
I'm not an expert at using SqlDataSource, but you might try inserting a SqlDataSource.Update in your Item_Updating method:
SqlDataSource2.UpdateParameters("cli_cod").DefaultValue = cliCod
SqlDataSource2.UpdateParameters("cli_desc").DefaultValue = cliDesc
SqlDataSource2.UpdateParameters("cli_user").DefaultValue = cliUser
SqlDataSource2.UpdateParameters("cli_pass").DefaultValue = cliPass
SqlDataSource2.UpdateParameters("cli_Admin").DefaultValue = If(cliAdmin = True, "1", "0")
SqlDataSource2.Update();
DetailsView1.DataBind()
I use a DetailsView in an ongoing project. They are finicky, but they work great when set up correctly.
Several potential issues I can see with your DetailsView are:
Eval vs Bind
As with all databound controls, Databinder.Eval is a read-only databind method. To allow controls to automatically update, you must use Bind in the edit template. Of course, you can manually update your database, but why reinvent the wheel?
<asp:TemplateField HeaderText="Codice Cliente">
<ItemTemplate>
<asp:Label ID="lblCliCod" runat="server" Text='<%# Eval("DataItem.cli_cod") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliCod" runat="server" Text='<%# Bind("DataItem.cli_cod")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Built-in Update functionality vs manual update functionality
You don't really need to use DetailsView1_ItemUpdating unless you are want to catch and modify values after submit and before committing to the datasource. i.e. concatenating individual year/month/day textboxes into a single date.
However, since you are not currently generating an Update button (using property AutoGenerateEditButton="True"), you may need to handle the update manually as you are attempting. Alternatively, for basic binding and update, I would comment out the updating functions, add the autogenerateeditbutton="True" property, and then try updating.
Primary Key
With some datasources, you must specify the DataKeyNames property (your primary key/s of your datasource) on the DetailsView to enable auto-binding. I don't know for sure, but you may also need to set the DefaultMode property.
All properties and methods are explained well on the MSDN DetailsView page, but it's rather verbose. The MSDN Updating a DetailsView page is more concise

CollapsiblePanelExtender in nested Repeater and Gridview setup doesn't function

I have a setup using three levels of nesting: A Repeater, the items of which utilize CollapsiblePanelExtenders (which work), and each contain a GridView. Each of these then contain another GridView which is controlled by another CollapsiblePanelExtender. These inner CollapsiblePanels will effectively show either an expanded or collapsed state only if I set clientState to True. However, they do not effectively expand or collapse as expected. Everything is bound dynamically.
Here is the markup...
<asp:Repeater ID="cat_repeater" runat="server">
<ItemTemplate>
<asp:CollapsiblePanelExtender id="cat_cpExt" runat="server" TargetControlID="cat_pnl" CollapsedSize="0" Collapsed="false" CollapsedImage="collapsed.png" ExpandedImage="expanded.png" ExpandControlID="cpControl_pnl" CollapseControlID="cpControl_pnl" TextLabelID="cpControl_lbl" ImageControlID="cpControl_img" CollapsedText='<%#configCPText(eval("Title"), False)%>' ExpandedText='<%#configCPText(eval("Title"), True) %>' />
<asp:Panel ID="cpControl_pnl" runat="server" Visible='<%#itemVisible(eval("ID"), "Recipients", "CategoryID") %>' CssClass="CPanelStyle">
<asp:Image ID="cpControl_img" runat="server" ImageUrl="expanded.png" />
<asp:Label ID="cpControl_lbl" runat="server" Text='<%#configCPText(eval("Title"), True) %>' CssClass="CPanelText" />
</asp:Panel>
<asp:Panel ID="cat_pnl" runat="server">
<asp:GridView ID="recipients_gv" runat="server" CssClass="GVStyle" HeaderStyle-CssClass="GVHeaderStyle" RowStyle-CssClass="GVItemStyle" AutoGenerateColumns="false" GridLines="none" AllowPaging="false">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="Last Name" ItemStyle-CssClass="GVNameStyle">
<ItemTemplate>
<asp:Literal id="name_lit" runat="server" text='<%#formatNameText(eval("FirstName"), eval("LastName")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gifts" ItemStyle-Width="500px">
<ItemTemplate>
<asp:CollapsiblePanelExtender id="gifts_cpExt" runat="server" TargetControlID="gifts_pnl" CollapsedSize="0" Collapsed="true" CollapsedImage="collapsed.png" ExpandedImage="expanded.png" ExpandControlID="cpControl_pnl2" CollapseControlID="cpControl_pnl2" TextLabelID="cpControl_lbl2" ImageControlID="cpControl_img2" CollapsedText='<%#configGiftsCPText(eval("ID"), True)%>' ExpandedText='<%#configGiftsCPText(eval("ID"), False) %>' />
<asp:Panel ID="cpControl_pnl2" runat="server" Visible='<%#itemVisible(eval("ID"), "Gifts", "RecipientID") %>'>
<asp:Image ID="cpControl_img2" runat="server" ImageUrl="collapsed.png" />
<asp:Label ID="cpControl_lbl2" runat="server" Text='<%#configGiftsCPText(eval("ID"), False) %>' />
</asp:Panel>
<asp:Panel ID="gifts_pnl" runat="server">
<asp:GridView ID="gifts_gv" runat="server" DataKeyNames="ID" RowStyle-CssClass="GVInnerItemStyle" HeaderStyle-CssClass="GVInnerHeaderStyle" Gridlines="None" AutoGenerateColumns="false" AllowPaging="false" Width="475px">
<Columns>
<asp:TemplateField ItemStyle-CssClass="GVInnerButtonItemStyle" HeaderText="Description">
<ItemTemplate>
<asp:LinkButton ID="gift_lBtn" runat="server" Text='<%#eval("Description") %>' CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Complete" ItemStyle-Width="50px">
<ItemTemplate>
<asp:CheckBox ID="giftComplete_cbx" runat="server" Checked='<%#eval("Complete") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
<br />
</ItemTemplate>
</asp:Repeater>
...And here is the code to bind everything:
Protected Sub bindCategories()
Try
oCmd.Connection = oConn
oCmd.CommandType = CommandType.Text
strSQL = "SELECT * FROM Categories"
oCmd.CommandText = strSQL
oDA.SelectCommand = oCmd
oDA.Fill(oDTbl)
cat_repeater.DataSource = oDTbl
cat_repeater.DataBind()
For i As Integer = 0 To oDTbl.Rows.Count - 1
oCmd.Parameters.Clear()
inner_dTbl.Clear()
strSQL = "SELECT *, Title FROM Recipients INNER JOIN Categories on Recipients.CategoryID = Categories.ID WHERE CategoryID = #CategoryID ORDER BY LastName"
oParam = New SqlParameter
oParam.ParameterName = "CategoryID"
oParam.SqlDbType = SqlDbType.Int
oParam.Value = oDTbl.Rows(i)("ID")
oCmd.Parameters.Add(oParam)
oCmd.CommandText = strSQL
oDA.SelectCommand = oCmd
oDA.Fill(inner_dTbl)
Dim gv As GridView = CType(cat_repeater.Items(i).FindControl("recipients_gv"), GridView)
gv.DataSource = inner_dTbl
gv.DataBind()
For j As Integer = 0 To inner_dTbl.Rows.Count - 1
oCmd.Parameters.Clear()
gifts_dTbl.Clear()
strSQL = "SELECT * FROM Gifts WHERE RecipientID = #RecipientID ORDER BY Description"
oParam = New SqlParameter
oParam.ParameterName = "RecipientID"
oParam.SqlDbType = SqlDbType.Int
oParam.Value = inner_dTbl.Rows(j)("ID")
oCmd.Parameters.Add(oParam)
oCmd.CommandText = strSQL
oDA.SelectCommand = oCmd
oDA.Fill(gifts_dTbl)
Dim inner_gv As GridView = CType(gv.Rows(j).FindControl("gifts_gv"), GridView)
inner_gv.DataSource = gifts_dTbl
inner_gv.DataBind()
Dim cpExt As CollapsiblePanelExtender = CType(gv.Rows(j).FindControl("gifts_cpExt"), CollapsiblePanelExtender)
cpExt.Collapsed = True
cpExt.ClientState = True
Next
Next
Catch ex As Exception
Throw ex
End Try
End Sub
I've successfully used CollapsiblePanelExtenders in a 2-level nested GridView setup that without a problem before, and without having to set clientState. However, I've had to specify clientState when using CollapsiblePanelExtenders with Repeaters before.
Does anyone have any input regarding why these extenders will not function in this 3-level nested setup?
It looks like the reason it's required is because it needs a postback to make all that logic work:
if (this.SupportsClientState) {
ScriptManager.RegisterHiddenField(this, this.ClientStateFieldID, this.SaveClientState());
this.Page.RegisterRequiresPostBack(this);
}
This is from public class ScriptControlBase in the AjaxControlToolkit space.
Are you wrapping this in an UpdatePanel? Does it cause a postback to use the CPE in the repeater? It's been awhile since I've tried to do a CPE in a repeater, and I'm not setup at this point to check the code out and build one.
Is there a reason you can't just use something completely client-side and just expand/collapse these using javascript? Are you loading data dynamically server side when these things get expanded?

Resources