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.
Related
I tried the below code for fetching values from text box present in gridview but the text value shows blank "".
what's the issue with this code??
TextBox box1 = (TextBox)grdCountry.Rows[rowIndex].Cells[0].FindControl("TextBox1");
design code :
<asp:gridview ID="grdCountry" runat="server" ShowFooter="true"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CountryName" HeaderText="Country" ItemStyle-Width="200px" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Try define a name for textbox, and in c# use "nameTextBox.Text();"
The issue was resolved.The above code works properly.I was refreshing the whole page hence the data stored in textbox was showing blank data.
I have the following code ...
<asp:DetailsView ID="dvApprenticeship" runat="server" DataSourceID="dsApprenticeship" AutoGenerateRows="false" BackColor="#E0E8F0" GridLines="None" CellPadding="2"
DataKeyNames="ProgramID, ProgramName, OrganisationName, StudyYearID, Workgroup, Pathway, FinishDate" OnDataBound="Apprenticeship_DataBound">
<Fields>
<asp:BoundField DataField="ProgramName" HeaderText="Program:" />
<asp:BoundField DataField="StudyYearName" HeaderText="Study Year:" />
<asp:HyperLinkField DataTextField="OrganisationName" HeaderText="Apprenticeship: " NavigateUrl="Apprenticeships.aspx" />
<asp:BoundField DataField="Workgroup" HeaderText="Workgroup:" />
<asp:BoundField DataField="Pathway" HeaderText="Pathway:" />
<asp:TemplateField HeaderText="Nominal Completion: ">
<ItemTemplate>
<asp:Label ID="labEndDate" runat="server" Text='<%# Eval("FinishDate","{0:d/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FooterTemplate>
<asp:LinkButton ID="lbAddProgramUnits" runat="server" OnClick="AddProgramUnits_Click" ForeColor="White" Font-Bold="true"
OnClientClick="return confirm('Import the Program Units listed - this may overwrite unit dates. Are you sure?');">Import from Program</asp:LinkButton>
Help
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" BackColor="LightSlateGray" />
</asp:DetailsView>
I want to be able to show a tooltip whenever one of the above Boundfields has changed color.
In my C# codebehind, I have code that changes the color of these boundfields depending on certain conditions of the data. This is working fine.
But what I want is to be able to give the users a tooltip when ever they hover their mouse over these Boundfields and ONLY if that field is coloured differently, in my case
color.Yellow
.
And to answer my own question, with something else I found, which was overlooked: the convert BoundField to TemplateField option.
From this ...
<asp:BoundField HeaderText="Claim Type ID" ..etc../>
To This ...
<asp:TemplateField HeaderText="Claim Type ID">
<EditItemTemplate>
<asp:Label ID="lblClaimTypeID" runat="server" Text='<%# Eval("ClaimTypeID") %>' ToolTip="Enter a numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtClaimTypeID" runat="server" Text='<%# Bind("ClaimTypeID") %>' ToolTip="Enter a numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="itClaimTypeID" runat="server" Text='<%# Bind("ClaimTypeID") %>' ToolTip="A numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is sweet, because in the Design mode of ASPX, you can select your DetailsView, select the Edit Fields option and select the fields that are BoundFields and convert them straight into TemplateFields. The beauty with that, is that it converts the BoundFields to neat Labels or TextBoxes allowing you to directly code in a ToolTip property! And no code behind! Microsoft got something right there for once.
If you are setting the color to yellow in the DetailsView DataBound event based on some criteria, you can set the tooltip in that same block:
DetailsViewRow.Cells[indexofyellowfield].ToolTip = "some help from code-behind";
i'm trying to update my asp.net gridview from a close method of a Growl Message.
the jquery code is:
$.gritter.add({
title: 'Success',
text: msg.d[0],
sticky: true,
after_close: function () {
__doPostBack('UpdatePanel1', '');
}
});
the gridview update panel is:
<div class="module_content">
<asp:Label runat="server" ID="Label1" />
<asp:GridView ID="grd1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Hyperlink href="#" id="getSessionID" class="view-details" runat="server">View details</asp:Hyperlink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Message Id">
<ItemTemplate>
<%# Eval("SessionID") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sender">
<ItemTemplate>
<%# Eval("Sender") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Message Date">
<ItemTemplate>
<asp:HyperLink CssClass="msgDateDate" ID="lnkMsgDateSummary" Text='<%#DateTime.Parse(Eval("MsgDate").ToString()).ToShortDateString()%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Message Type">
<ItemTemplate>
<asp:HyperLink CssClass="msgDateType" ID="lnkMsgDateType" Text='<%# Eval("MsgType")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reference">
<ItemTemplate>
<%# Eval("MsgRef")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<%# Eval("Status")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Within the gridview is a hyperlink, which opens up a dialog, the dialog calls a webmethod, which results in the data of the gridview changing.
I use a Growl notification to inform the end user that something has changed, and on click the ok button the after_close function is called.
When I debug the OnLoad=UpdatePanel1_Load is called, and I can see that all that changes are being bound etc.
However, my GridView total vanishes after the after_close method is run, it's not even in the DOM anymore.
Oddly though the label is there!
Can anyone tell me why my Grid vanishes?
*EDIT*
Ok, so i've added the EmptyDataText to the gridview, and the text specified in here is visible. So, even though a call the bindmethod my Gridview is empty!
On your page load event, you need to implement a check when ispostback is true, then do not load the page to default value i.e. binding of grid view.
Hope this helps.
The problem was user error. I had a case statment which was loading hte grids data based on date selected in a drop down, then drop down menu was empty, therefore my code was performing as expected, i/e no data to display you plonker :-)
I have a gridview column in which I have one column that point to a pdf file on the file server. I need to have another column right beside this, displaying the pdf icon. The user should be able to click the icon and launch the file from the file server.
My code is:
<asp:GridView ID="gvInvoices" AutoGenerateColumns="false" runat="server" Font-Names="Arial" Font-Size="Small" Width="50%">
<Columns>
<asp:TemplateField HeaderText="File Type">
<ItemTemplate><img runat="server" src="Images/img_Pdf.gif" id="imgFileType" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate><%#Eval("InvoiceNumber")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I add a "NavugateURL" or href="" to the img ?
Rather than using an HTML img control, try using an ASP.Net Server control.
A good choice would be the Hyperlink Control:
Instead of:
<img runat="server" src="Images/img_Pdf.gif" id="imgFileType" />
Use:
<asp:HyperLink ID="imgFileType" ImageUrl="Images/img_Pdf.gif" NavigateUrl="" runat="server">HyperLink</asp:HyperLink>
Just set your NavigateUrl property.
you need to wrap your icon around an anchor tag, and set the href of the anchor tag using DataBinding expression Eval. This assumes your Datasource field "PDFPath" is an absolute path.
<asp:GridView ID="gvInvoices" AutoGenerateColumns="false" runat="server" Font-Names="Arial" Font-Size="Small" Width="50%">
<Columns>
<asp:TemplateField HeaderText="File Type">
<ItemTemplate><a href='<%#Eval("PDFPath")%>'> <img runat="server" src="Images/img_Pdf.gif" id="imgFileType" /></a></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate><%#Eval("InvoiceNumber")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
inside the RowDataBind event look for your image control and set its NavigateUrl property
protected void gvInvoices_RowDataBound(object Sender , GridViewRowEventArgs e)
{
if(e.Row.RowType==DataRow)
{
HtmlControl icon = e.Row.FindControl("imgFileType") as HtmlControl;
icon.NavigateUrl = ((MyDataType)e.Row.DataRow).PDFPath;
}
}
Note it is free hand writing so you may find some syntax errors that you should fix
I want to set hyperlink field in datagrid view. When user clicks on that link, a query string should be generated and user should be directed to another page. So how can I set hyperlink to generate query string?
<asp:GridView ID="Griddata" runat="server" AutoGenerateColumns="False" CellPadding="1"
GridLines="Horizontal" Width="1000px" ShowFooter="True" CssClass="grid" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:HyperLinkField HeaderText="ID" DataTextField="rec_id" DataNavigateUrlFields="rec_id"
DataNavigateUrlFormatString="followme.aspx?record={0} " />
<asp:BoundField HeaderText="Login" DataField="LoginName"></asp:BoundField>
</Columns>
</asp:GridView>
This is a sample GridView defined in ASP.NET
You need to specify the <asp:Hyperlinkfield> in the column definition.
In that field, you need to specify the DataTextfield (is what will be displayed on screen in that column), your URL (DataNavigateUrlFormatString) and your parameter that you want to use in that URL (DataNavigateUrlFields)
Note: I'm binding to this grid from code-behind, not through a SqlDatAdaptor but the result is the same.
You will get something like this:
you can do like...
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
<a href='page.aspx?id=<#Eval("ID")>'>click</a>