I have an asp.net application in which i have to add a warning javascript message when i click into button in my Gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:HiddenField ID="Ref" runat="server" value='<%#Eval("Ref")%>' ></asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:Button ID="Savebtn" runat="server" Text="update" OnClick="gv_RowEditing"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Note">
<ItemTemplate>
<asp:TextBox ID="note" runat="server" Text='<%#Eval("Note")%>' onfocus="this.style.color='red'" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="max">
<ItemTemplate>
<asp:TextBox ID="maxtxt" runat="server" Text='<%#Eval("prix max")%>' onfocus="this.style.color='red'"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="min" >
<ItemTemplate>
<asp:TextBox ID="mintxt" runat="server" Text='<%#Eval("prix min")%>' onfocus="this.style.color='red'"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<EditRowStyle BorderColor="Yellow" Font-Underline="True" ForeColor="#FF3300" />
</asp:GridView>
I need to add an alert when i click into the button savebtn, if maxtxt value is less than mintxt value , an warning message appears.
How can i do this?
What is the differents ways to add an alert message to an aspx page?
Use the OnClientClick event to call a Javascript method to validate your fields.
<asp:Button ID="Savebtn" runat="server" Text="update" OnClick="gv_RowEditing"
OnClientClick="if ( ! ValidateFields()) return false;"/>
Your javascript method: (Not 100% sure of syntax but you get the idea)
function ValidateFields() {
var maxtxt = document.getElementById("<%=maxtxt.ClientID%>").Value;
var mintxt = document.getElementById("<%=mintxt.ClientID%>").Value;
if ( maxtxt < mintxt ) {
alert("This is bad");
return false;
}
return true;
}
Use the OnClientClick function of the Button to specify some JavaScript to run before the postback occurs.
<asp:Button ID="Button1" runat="server" Text="Delete" OnClientClick='MyFunction();' OnClick="Button1_Click" />
function MyFunction()
{
var maxtxt = document.getElementById("<%=maxtxt.ClientID%>").Value;
var mintxt = document.getElementById("<%=mintxt.ClientID%>").Value;
if ( maxtxt < mintxt ) {
alert("You did something wrong!");
}
}
Often, you simply want to confirm some action on the client side before it gets to the server side
<asp:Button ID="Button1" runat="server" Text="Delete" OnClientClick='return confirm("Are you sure you want to delete this item?");' OnClick="Button1_Click" />
You can combine the two...
<asp:Button ID="Button1" runat="server" Text="Delete" OnClientClick='return MyFunction();' OnClick="Button1_Click" />
function MyFunction()
{
var maxtxt = document.getElementById("<%=maxtxt.ClientID%>").Value;
var mintxt = document.getElementById("<%=mintxt.ClientID%>").Value;
if ( maxtxt < mintxt ) {
return confirm("Are you sure you want to do that");
}
return true;
}
<asp:Button ID="Savebtn" runat="server" Text="update" OnClick="gv_RowEditing"/>
function gv_RowEditing(){
if(parseInt($('#maxTxt').val()) < parseInt($('#minTxt').val())){
alert("you message");
}
}
or
$("#Savebtn").click(function() {
if(parseInt($('#maxTxt').val()) < parseInt($('#minTxt').val())){
alert("you message");
}
});
This is using jquery.
Define your gridview as follows:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:HiddenField ID="Ref" runat="server" value='<%#Eval("Ref")%>' ></asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:Button ID="Savebtn" runat="server" Text="update" OnClick="gv_RowEditing"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Note">
<ItemTemplate>
<asp:TextBox ID="note" runat="server" Text='<%#Eval("Note")%>' onfocus="this.style.color='red'" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="max">
<ItemTemplate>
<asp:TextBox ID="maxtxt" maxtxt="1" runat="server" Text='<%#Eval("prix max")%>' onfocus="this.style.color='red'"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="min" >
<ItemTemplate>
<asp:TextBox ID="mintxt" mintxt="1" runat="server" Text='<%#Eval("prix min")%>' onfocus="this.style.color='red'"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button CommandName="Update" Text="Update" OnClientClick="return Validate();" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BorderColor="Yellow" Font-Underline="True" ForeColor="#FF3300" />
</asp:GridView>
Then include the following js (using jquery):
function Validate(){
var max = $("input[maxtxt]").val();
var min = $("input[mintxt]").val();
if (maxtxt < mintxt){
alert("Something wrong!!");
return false;
}
else{
return true;
}
}
Related
I use a CalendarExtender of AjaxControlToolkit in a GridView and a TextBox. And from that TextBox I want to disable future dates.
I am trying below code
<asp:GridView ID="Gridview2" runat="server" AutoGenerateColumns="false" DataKeyNames="Sno" ShowFooter="true" OnRowCommand="Gridview2_RowCommand" CssClass="table table-bordered table-striped"
OnRowCancelingEdit="Gridview2_RowCancelingEdit" OnRowDeleting="Gridview2_RowDeleting" OnRowEditing="Gridview2_RowEditing" OnRowUpdating="Gridview2_RowUpdating" OnRowDataBound="Gridview2_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Sno" Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_sno1" runat="server" Text='<%#Eval("Sno") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of visit" ShowHeader="false">
<EditItemTemplate>
<asp:TextBox ID="Txt_update_DateofVisit1" runat="server" Text='<%# Eval("DateofVisit","{0:MM/dd/yyyy}") %>' CssClass="form-control"/>
<cc1:CalendarExtender ID="CalendarExtenderedit11" CssClass="Calendar" TargetControlID="Txt_update_DateofVisit1" Format="MM/dd/yyyy" runat="server"></cc1:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_DateofVisit1" runat="server" Text='<%# Eval("DateofVisit","{0:MM/dd/yyyy}") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_DateofVisit_add" runat="server" CssClass="form-control" />
<cc1:CalendarExtender ID="CalendarExtendere21" CssClass="Calendar" TargetControlID="txt_DateofVisit_add" Format="MM/dd/yyyy" runat="server"></cc1:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete_add" runat="server" CommandName="Delete" ImageUrl="~/img/grid_delete.png" Text="Edit" ToolTip="click here to delete" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd_add" runat="server" CommandName="AddNew" ImageUrl="~/img/grid_add.png" ToolTip="click here to add" CausesValidation="true" ValidationGroup="outadd" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Below is my RowDataBound
protected void Gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
AjaxControlToolkit.CalendarExtender CalendarExtendere21 = (AjaxControlToolkit.CalendarExtender)Gridview2.FooterRow.FindControl("CalendarExtendere21");
CalendarExtendere21.EndDate = DateTime.Now;
// CalendarExtendere21.StartDate = DateTime.Now;
}
}
I get below error:
Object reference not set to an instance of an object.
just use parameter e to find out control in grid
if (e.Row.RowType == DataControlRowType.Footer)
{
AjaxControlToolkit.CalendarExtender CalendarExtendere21 =
(e.Row.FindControl("CalendarExtendere21") as AjaxControlToolkit.CalendarExtender);
}
and don't forget to check for null
I am using ASP.NET and Entity Framework and Tab control of AJAX
I have a dropdown list control in gridview which is in TAB control Am not able to bind dropdown list control.
Tab Control --> Gridview --> Dropdown list
My ASP.NET Code is
<ajax:TabContainer ID="TabContainer2" runat="server" CssClass="fancy fancy-green">
<ajax:TabPanel ID="TabPanel1" runat="server">
<HeaderTemplate>
College Toppers
</HeaderTemplate>
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
<div>
<asp:GridView ID="GridView1" DataKeyNames="TL_ID" runat="server"
AutoGenerateColumns="False" CssClass="Gridview" ShowFooter="True" OnRowDataBound="GRVToppers_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Admin/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" /><asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Admin/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Admin/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Admin/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" /><asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Admin/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Topper List No">
<EditItemTemplate>
<asp:Label ID="lblEditTpid" runat="server" Text='<%#Eval("TL_ID") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblShowTpid" runat="server" Text='<%#Eval("TL_ID") %>' />
</ItemTemplate>
<ControlStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Department Name">
<EditItemTemplate>
<asp:DropDownList ID="ddlEditDeptname" runat="server" Width="90" DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="true"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddladdnewDeptname" runat="server" Width="90" DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="True"></asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblShowDeptname" runat="server" Text='<%#Eval("DEPT_NAME") %>' Width="90" />
</ItemTemplate>
<ControlStyle Width="30px" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White"></HeaderStyle>
</asp:GridView>
</div>
</asp:Panel>
</ContentTemplate>
</ajax:TabPanel>
</ajax:TabContainer>
==================
My C# code is
protected void GRVToppers_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
var dept = from n in ecme.DEPT_MASTER
select new { n.DEPT_ID, n.DEPT_NAME };
DropDownList ddl = null;
if (e.Row.RowType == DataControlRowType.Footer)
{
ddl = e.Row.FindControl("ddladdnewDeptname") as DropDownList;
int cnt = 0;
cnt = dept.Count();
ListItem[] items = new ListItem[cnt];
for (int i = 0; i < cnt; i++)
{
items[i] = new ListItem("DEPT_NAME", "DEPT_ID");
}
}
}
catch (Exception)
{ }
}
========================
You can do this very easily...
Remove
DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="True"
and
DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="true"
from aspx code
and Make changes in
protected void GRVToppers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
var dept = from n in ecme.DEPT_MASTER
select new { n.DEPT_ID, n.DEPT_NAME };
DropDownList ddl = (DropDownList)e.Row.FindControl("ddladdnewDeptname");
ddl.DataTextField = "DEPT_NAME";
ddl.DataValueField = "DEPT_ID";
ddl.DataSource = dept.ToList();
ddl.DataBind();
ddl.Items.Insert(0, "--Select Department--");
}
}
100% sure this 'll work...
am editing and updateing the gridview columns but it not taking the values. and it not show the error.
plz tell the suggestion. how to do the updated. if am click on edit it enable. when am enter the values it not updating the db.
if (ViewState["LeaveManagementID"] == null)
{
foreach (GridViewRow row in gvLeaves.Rows)
{
EP_LeaveManagementRow leaveUpdateRow = db.EP_LeaveManagementCollection.GetRow("LeaveManagementID=" + gvLeaves.DataKeys[e.RowIndex].Value);
if (leaveUpdateRow != null)
{
leaveUpdateRow.NoOfDays = int.Parse(lblNoOfDays.Text.ToString());
db.EP_LeaveManagementCollection.Update(leaveUpdateRow);
ViewState["LeaveManagementID"] = leaveUpdateRow.LeaveManagementID;
mvLeaveManagement.SetActiveView(vwLeavesDetails);
ASPX
<asp:TemplateField HeaderText="Comments">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
<HeaderStyle Width="15%" HorizontalAlign="Left" VerticalAlign="Middle"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="lblComments" Visible="false" runat="server" ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtComments" runat="server" MaxLength="50" TextMode="MultiLine" Text='<%#Bind("LeaveCommants") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit/Update" HeaderStyle-Width="25%">
<ItemTemplate>
<asp:LinkButton ID="lkbEdit" Text="Edit" runat="server" CommandName="Edit" />
<%-- <asp:LinkButton ID="lkbUpdate" Text="Update" runat="server" CommandName="Update" />--%>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
I have ascx page in that I have a grid in side that I have added radio button, I want to check at least one checkbox should be checked before unload the page or clicking on btn save.
<asp:GridView ID="grdTest" runat="server" DataKeyNames="Id" AutoGenerateColumns="False" HorizontalAlign="Center" GridLines="None" onrowcreated="grdTest_RowCreated">
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="txtText" Rows="10" Columns="40" runat="server" TextMode="MultiLine" Height="55px" Text='<%# Eval("Text") %>'></asp:TextBox>
<asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Save"/>
<span id="checkbox">
<asp:RadioButton ID="rbtnTest" runat="server" Checked='<%# Eval("Correct") %>' TextAlign="Left" Height="28px" />
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtText" Rows="10" Columns="40" runat="server" TextMode="MultiLine" />
<asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Save"/>
<asp:CheckBox ID="rbtnTest" runat="server" Enabled="true" TextAlign="Left"/>
</EditItemTemplate>
</asp:TemplateField>
</asp:GridView>
How is this possible plz some one help me?
<script type="text/javascript" language="javascript">
function Validate_Checkbox()
{
var chks=document.getElementsByTagName('input');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one checkbox..!");
return false;
}
return true;
}
</script>
and on Submit Button you have to write
OnClientClick="return Validate_Checkbox()"
I have an issue with having a asp.net Grid View loaded into a div tag on a page (UI/Host.aspx). The Grid View itself is on a seperate page (GridViews/GridView1.aspx) and I'm using the jQuery load() function to load this page into the div tag.
My problem is when sorting the grid view it tries to postback to the page that's hosting it, and comes back with the error "Unable to find page UI/GridView1.aspx", is there a way to override this so that it post backs to itself, (which I assumed it would but doesn't) or is there an easier way to do the sorting.
Is there any other way of doing this, even if it means getting rid of the GridView altogether and using a repeater and table?
Below is the code:
UI/Hosts.aspx
//jQuery to load the div with the page UI/Hosts.aspx
$(document).ready(function() {
StartRefresh();
});
function startRefresh()
{
refreshID = setInterval(function() {
DisplayDate();
$("#divDests").load("../GridViews/Gridview1.aspx?ConfigID=" + $("#ctl00_MainContent_dlConfiguration").val() + "&ModuleID=" + $("#ctl00_MainContent_ddlModule").val());
}, $("#ctl00_MainContent_ddlRefresh :selected").val());
}
GridViews/Gridview1.aspx;
//Markup for GridViews/Gridview1.aspx
<html>
<head><title></title></head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<br />
<asp:GridView Font-Size="8.7pt" ID="gvLiveDest" runat="server" AutoGenerateColumns="False"
EmptyDataText="No Records Found" AllowSorting="true"
onsorting="gvLiveDest_Sorting" onrowcreated="gvLiveDest_RowCreated" OnRowDataBound="gvLiveDest_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="DestinationName" HeaderStyle-CssClass="centralalignment">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# WebHelper.HTMLSafe(Eval("Description")) %>' ToolTip='<%# WebHelper.HTMLSafe(Eval("Description")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logged <br /> In" HeaderStyle-CssClass="centralalignment" SortExpression="LoggedIn" >
<ItemStyle CssClass="centralalignment" />
<ItemTemplate>
<asp:Label ID="lblLoggedIn" runat="server" Text='<%# SetLoggedIn(Convert.ToBoolean(Eval("Active"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Current<br />Status" HeaderStyle-CssClass="centralalignment" SortExpression="LastStatus" >
<ItemStyle CssClass="centralalignment" />
<ItemTemplate>
<asp:Label ID="lblCurrentStatus" runat="server" Text='<%# WebHelper.HTMLSafe(Eval("LastStatus")) %>' ToolTip='<%#Eval("LastStatus") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time in<br />Current<br />Status" HeaderStyle-CssClass="centralalignment" SortExpression="CurrentDuration">
<ItemStyle CssClass="RightAlignment" />
<ItemTemplate>
<asp:Label ID="lblCurrentTime" runat="server" Text='<%# ICT.DAL.Reporting.CallDurFormat(Eval("CurrentDuration")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Lines" HeaderText="Lines" HeaderStyle-CssClass="centralalignment" SortExpression="Lines"
ItemStyle-CssClass="centralalignment" />
<asp:BoundField DataField="LinesBusy" HeaderText="Lines <br /> Busy" HeaderStyle-CssClass="centralalignment"
ItemStyle-CssClass="centralalignment" ReadOnly="True" HtmlEncode="False" SortExpression="LinesBusy" />
<asp:BoundField DataField="LinesAvailable" HeaderStyle-CssClass="centralalignment"
ItemStyle-CssClass="centralalignment" SortExpression="LinesAvailable"
HeaderText="Lines <br /> Available" HtmlEncode="false" ReadOnly="True" />
<asp:TemplateField HeaderText="Last Call Time" SortExpression="Timestamp" HeaderStyle-CssClass="centralalignment">
<ItemTemplate>
<asp:Label ID="lblLastCallTime" runat="server" Text='<%# WebHelper.HTMLSafe(Eval("LastCallTime")) %>' ToolTip='<%# WebHelper.HTMLSafe(Eval("LastCallTime")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
And the onSort Event Code (However it never hits this)
protected void gvLiveDest_Sorting(object sender, GridViewSortEventArgs e)
{
if (string.Compare(e.SortExpression, ViewState["SortField"].ToString(), true) == 0)
{
_sortDir = (_sortDir == "ASC") ? "DESC" : "ASC";
}
else
_sortDir = "ASC";
_SortField = e.SortExpression;
ViewState["SortField"] = e.SortExpression;
ViewState["sortDir"] = _sortDir;
BindLiveDestination();
}
I switched over to client-side paging/sorting a while ago and haven't been happier. Of course, you would need to set AllowSorting="false" and AllowPaging="false" in your GridView.
You could put it into an iframe...