I've done this a million times without a problem, and I'm stumped at this one.
I have a DataGrid:
<asp:DataGrid ID="gridDeptEmployees" runat="server" AutoGenerateColumns="False"
ItemStyle-CssClass="hovertable" ViewStateMode="Enabled">
<ItemStyle Font-Size="Smaller" />
<Columns>
<asp:TemplateColumn HeaderStyle-CssClass="hidden">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="firstname" HeaderStyle-CssClass="hidden" />
<asp:BoundColumn DataField="lastname" HeaderStyle-CssClass="hidden" />
<asp:BoundColumn DataField="employeeNumber" HeaderStyle-CssClass="hidden" />
</Columns>
</asp:DataGrid>
This datagrid is bound to a list of objects:
Dim userList as New List(Of User)
userList = getList()
gridDeptEmployees.DataSource = ulst
gridDeptEmployees.DataBind()
I can see users in the list when I set a breakpoint and step through it, but the grid is never visible. The visible property is always false!
I've tried explicitly setting the visible property, but it doesn't honor my changes. I even tried adding my DataKeyNames, but that didn't work.
What am I missing here? Should I switch to a different control?
When you can not see a control (not rendered on page), and debug it and see that the visible is false, then is probably because is inside some other hidden control, like a PlaceHolder, or a Panel, or a UserControl.
Check to see if a parent control is hidden to find your reason.
Related
I want to access "Checked" property against a checkbox control in GridView, please see my aspx page code below firstly:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteAll" runat="server" OnCheckedChanged="cbDeleteAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" OnCheckedChanged="cbDelete_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lblEmpId" runat="server" Text='<%# Bind("DeptID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" />
<asp:BoundField DataField="DeptID" HeaderText="Department" />
</Columns>
</asp:GridView>
in the "cbDeleteAll_CheckedChanged" event, I would like to see the Checked property on the header template and set value to Checked property to checkboxes in the item template, see below:
as you can see, there is a red wave line under "Checked" property, when I rebuild the solution, I can see the error message as below:
so, I was totally confused I don't know why I cannot access this property in the code?
can anyone help me or give me some instructions?
Look at the error. It says WebApplication1.CheckBox. It appears you have a CheckBox class in your app and therefore you're casting to the wrong type. Don't name your classes the same thing as something built into the framework.
To find the offending file put your cursor over CheckBox in your class and press Function 12 or right click it and select Go To Definition. Then right click that class and rename it something more appropriate.
The correct CheckBox class is in System.Web.UI.WebControls namespace.
This is very strange. I have a gridview with item template checkboxes and buttons in each row. I am toggling the text value of the buttons (using JQuery) representing the user adding rows to a cart of items for purchase. I have a button on the form seperate from the gridview which when clicked raises an event in the code behind to spin through the rows and process the selected (toggled button text) rows. But what is happening is upon pressing the process button the grid rows are reverting back to their original states (original button text and also modified line attributes for the previously selected rows). I am not rebinding the grid. This is also not an update grid. Now the checkboxes have always worked just fine but the button text is obviously very different. Can anybody tell me why this is happening and how I may correct it? One idea I have is to create a column of hidden checkboxes and when I toggle my button text, I can toggle the checkboxes as well. But I sure wish I could understand why this is happening. Also, on the postback from the process button having been checked, the screen is rendered in it's original state as well.
Here is my grid with the checkboxes and buttons:
<div class="OneHundredPercentWide" style="padding-top:7px">
<asp:Panel ID="Panel1" runat="server" Width="100%" >
<asp:GridView id="grvSessionOrderDownloads" runat="server" BorderWidth="2px" BorderStyle="Solid" BorderColor="#C0C0FF"
Font-Names="Arial" Font-Bold="True" Font-Size="8pt" GridLines="Horizontal" AutoGenerateColumns="False"
HorizontalAlign="Left" ForeColor="Blue" CellPadding="2" AllowSorting="True" Width="100%"
OnRowDataBound="grvSessionOrderDownloads_OnRowDataBound" DataKeyNames="SORD_ID">
<HeaderStyle Font-Names="Arial" HorizontalAlign="Center" ForeColor="White" BackColor="#152EE5"
CssClass="sessionOrderDownloadHeaders" />
<RowStyle BackColor="White" ForeColor="#333333" />
<AlternatingRowStyle BackColor="#A5B0FF" />
<Columns>
<asp:BoundField Visible="False" DataField="SORD_ID" HeaderStyle-Width="0px" >
<HeaderStyle Width="0px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Select <br /> Files" HeaderStyle-ForeColor="White" >
<ItemTemplate >
<asp:CheckBox ID="chkSelectVideo" runat="server" OnClick="checkboxClicked(this)" ToolTip="Select file for download"
Enabled='<%# Eval("SORD_EnableSelectionCheckBox") %>' Checked='<%# Eval("SORD_SelectedForDownloadFlag") %>' />
</ItemTemplate>
<HeaderStyle Width="6%" />
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField><asp:TemplateField HeaderText="Prior <br /> Downld" HeaderStyle-ForeColor="White" >
<ItemTemplate >
<asp:Button id="btnBuy" runat="server" OnClientClick="btnBuyToggle(this); return false;"
Text="BUY This" CssClass="buyButton" Visible='<%# Eval("SORD_ShowBuyButton") %>' />
</ItemTemplate>
<HeaderStyle Width="7%" />
<ItemStyle CssClass="sessionOrderDownloadItems" VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is part of my code behing to check the button text:
Dim gvr As GridViewRow
For Each gvr In grvSessionOrderDownloads.Rows
Dim selBtn As Button = CType(gvr.FindControl("btnBuy"), Button)
If Not selBtn Is Nothing Then
If selBtn.Enabled And selBtn.Visible Then
Dim strSORD_ID As String = grvSessionOrderDownloads.DataKeys(gvr.RowIndex)("SORD_ID").ToString
If selBtn.Text = "BUY Video" Then
// Note: I will process here
End If
End If
End If
Next
Thank you,
Jim
The checkbox changes are posted back to the server once they are changed in the browser. But when you change the text of the button (or other fields) in the grid using JS, the changed button text (or other fields) is not sent to the server. Hence when the button is rendered in the next post back, it uses the original default value which is "BUY This".
In order to retain the button text, you would have to create a hidden field along with the button. When you change the text of the button (in OnClientClick JS event), change the value of the hidden field simultaneously. The hidden field changes are sent to the server which helps you to read them on the process button click event.
I have a gridview that I populate based on a query to a db. I'm trying to add sorting on my gridview but I cannot make my column headers clickable. I have allow sorting set to true, and I have my OnSorting event set. My columns are of a few different types. I know the code I need to have in my code behind, but I cannot click on the headers for some reason. Any help on what I'm missing would be appreciated.
<asp:GridView ID="Grid1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanging="Selected_Row_Changing"
DataKeyNames="ApplicationId"
AllowPaging="True"
OnPageIndexChanging="Grid1_PageIndexChanging"
AllowSorting="True"
OnSorting="Grid1_Sorting"
OnRowCreated="OnRowCreated"
OnRowCommand="Grid1_RowCommand"
OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:templatefield ...>
<itemtemplate>
<asp:linkbutton .../>
</itemtemplate>
</asp:templatefield>
<asp:BoundField ... />
<asp:HyperLinkField ... />
<asp:ButtonField ... />
</Columns>
</asp:GridView>
You don't have set the SortExpression, have you?
For example:
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
Make sure you're not setting the Header Template but rather set the HeaderText attribute for the TemplateField
In a grid view i have used paging for that i have used the view state to store datatale to bind it on GridView1_PageIndexChanging event every thing works fine but the problem happens with the first column which is having the checkbox placed in each row .
On navigation all checked check box becomes unchecked how to maintain the state of check box as well.
this is the aaspx code
<Columns>
<asp:TemplateField HeaderText="Select Student">
<ItemTemplate>
<asp:CheckBox id="Chek" runat="server" Text="select" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Enrollment No." DataField="enrollment_no" />
<asp:BoundField HeaderText="Course Name" DataField="course_name"/>
<asp:BoundField HeaderText="Branch Name" DataField="branch_name"/>
<asp:BoundField HeaderText="Email Id" DataField="email" />
<asp:BoundField HeaderText="Mobile" DataField="mobile"/>
<asp:BoundField HeaderText="Name" DataField="first_name"/>
<asp:BoundField HeaderText="Surname" DataField="last_name" />
</Columns>
</asp:GridView>
Viewstate is intended for postbacks to the same page.
To preserve state when navigating to other pages here are 3 options:
Put your checkbox (or simply true/false) in the Session
Use the PreviousPage property
Or use cookies
Summary of option #2
If you have ot post accross pages,
cookies can be used, and also Cross
Page Posting by setting the
PostBackURL property of a button, then
the POST request is directed at the
specified page, and you can get the
values from the PreviousPage property
of the next page.
Example of using option #3, the Session:
//Set
Session["mySessionVariableName"] = myCheckBox;
//Get
CheckBox myCheckBox = (CheckBox)Session["mySessionVariableName"];
I summarized in more detail here and here
take a look at this sample code: (question bellow)
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource2"
AutoGenerateColumns="False" onrowupdated="GridView1_RowUpdated"
DataKeyNames="Product_Id">
<Columns>
<asp:ImageField DataImageUrlField="Image_Name" HeaderText="Image_Name"
ReadOnly="True" >
<ItemStyle Width="50px" Height="50px" Wrap="true"/>
</asp:ImageField>
<asp:BoundField DataField="Product_Id" HeaderText="Product_Id"
InsertVisible="False" ReadOnly="True" SortExpression="Product_Id">
</asp:BoundField>
<asp:BoundField DataField="Product_Name" HeaderText="Product_Name"
SortExpression="Product_Name" />
<asp:BoundField DataField="Category_Name" HeaderText="Category_Name"
SortExpression="Category_Name" ReadOnly="true" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Size" HeaderText="Size"
SortExpression="Size" />
<asp:BoundField DataField="Price" HeaderText="Price"
SortExpression="Price" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Assume I initialize an SqlDataSource, add a parameter and so on.
The thing is, that when a user clicks edit we get a textbox to edit the colnumn value.
I want to validate the data enter by the user before the update is performed and the new
data is propagated back to the server.How?
10x a lot!
You need to convert the BoundField into a TemplateField. Then you can add a validator to the actual TextBox control.
Option 1:
But from the UNKNOWN answer, the Microsoft recommends the same.. as he told.
ref: http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic2
Option 2:
But, we can do.
You need to add the validation either the javascript validation or server side validation
control, when the GridView's DataBound event is happening at runtime on the particular
TableCell of the Gridview rows.
Hence, when you click the update button that custom generated javascript or the validation
control will check for the validation on editing the values.
This process is more harder than the converting boundfield to templatefield
refer: http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx
Option 3:
And you can go for server side validation on the values instead of client side validation:
refer: http://msdn.microsoft.com/en-us/library/bb332383.aspx