Imagebutton not giving an index off gridview so modal cant open - c#

Hi i have a gridview with a button on each row that opens a modal and it works. For testing purposes i gave my imagebutton the same commandName to see if it would also open the modal. Unfortunately it did not...
After a little debugging i found that the imagebutton does not return a index (the row number) so my details modal can not display as i get error:
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: Input string was not in a correct format.
I was wondering how i can pass the index number of the row using imagebutton.
I have included my code below:
markup
<asp:UpdatePanel ID="UserGridViewControl" runat="server">
<ContentTemplate>
<asp:GridView ID="GridViewUsers" runat="server" Width="940px" HorizontalAlign="Center"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false" AllowPaging="true"
DataKeyNames="Ref" CssClass="table table-hover table-striped" OnPageIndexChanging="GridViewUsers_PageIndexChanging"
AllowSorting="true" OnSorting="GridViewUsers_Sorting">
<Columns>
<asp:BoundField DataField="Ref" SortExpression="Ref" HeaderText="Ref #" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Listed" SortExpression="Listed" HeaderText="Listed" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="DateListed" dataformatstring="{0:MMMM d, yyyy}" SortExpression="DateListed" HeaderText="Date Listed" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Type" SortExpression="Type" HeaderText="Type" HeaderStyle-HorizontalAlign="Left" />
<asp:ButtonField CommandName="editRecord" ControlStyle-CssClass="btn btn-info" ButtonType="Button" Text="View Services" HeaderText="Additional Services">
<ControlStyle CssClass="btn btn-info"></ControlStyle>
</asp:ButtonField>
<asp:BoundField DataField="London" SortExpression="London" HeaderText="London" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="MoveInDate" dataformatstring="{0:MMMM d, yyyy}" SortExpression="MoveInDate" HeaderText="Movie in Date" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" CommandName="detail" ButtonType="Button" runat="server" />
<asp:ImageButton ID="ImageButton2" runat="server" />
<asp:ImageButton ID="ImageButton3" runat="server" />
<asp:ImageButton ID="ImageButton4" runat="server" />
<asp:ImageButton ID="ImageButton5" runat="server" />
<asp:ImageButton ID="ImageButton6" runat="server" />
<asp:ImageButton ID="ImageButton7" runat="server" />
<asp:ImageButton ID="ImageButton8" runat="server" />
<asp:ImageButton ID="ImageButton9" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="detail" ControlStyle-CssClass="btn btn-info" ButtonType="Button" Text="Detail" HeaderText="Details">
<ControlStyle CssClass="btn btn-info"></ControlStyle>
</asp:ButtonField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
Codebehind
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("detail"))
{
LoadSortedDB();
int index = Convert.ToInt32(e.CommandArgument); // Breaks Here as there is no value in command argument
Int32 LoweredEmailCheck = Convert.ToInt32(GridViewUsers.DataKeys[index].Value);
IEnumerable<DataRow> query = from i in dt.AsEnumerable()
where i.Field<Int32>("Ref").Equals(LoweredEmailCheck)
select i;
DataTable detailTable = query.CopyToDataTable<DataRow>();
DetailsView1.DataSource = detailTable;
DetailsView1.DataBind();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(#"<script type='text/javascript'>");
sb.Append("$('#detailModal').modal('show');");
sb.Append(#"</script>");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "DetailModalScript", sb.ToString(), false);
}
}

use find to get object of asp ajax modal popup
with id:
var modalobj=$find("<%=modalid.ClientID%>");
modalobj.show();
with Behaviourid:
var modalobj=$find("Behaviourid");
modalobj.show();
Working with your code:
sb.Append(#"<script type='text/javascript'>");
sb.Append("$find('detailModal').show();");
sb.Append(#"</script>");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "DetailModalScript", sb.ToString(), false);
Finding modal popup with Behaviourid

Related

How to retrieve the value in GridView when I click on LinkButton of GridView

I am using gridview to display of data from database, in each row of the gridview I am having delete and edit link button. How can I get value of “NAME” and "DESCRIPTION" when I click on Delete or Edit Button in gridview.
The following is my code.
List.aspx
<div align="center" style="margin-top:50px">
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" CellPadding="5" runat="server">
<Columns>
<asp:BoundField HeaderText="Report Name" DataField="NAME" />
<asp:BoundField HeaderText="Report Description" DataField="DESCRIPTION" />
<asp:BoundField HeaderText="Report Group" DataField="REPORT_GROUP" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" OnClick="Btn_Delete_Click" />
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
List.aspx.cs
protected void Btn_Delete_Click(object sender, EventArgs e)
{
}
use command argument like this
<ItemTemplate>
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandArguman='<%# Eval("Name")+","+Eval("DESCRIPTION") %>' OnClick="Btn_Delete_Click" />
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandArguman='<%# Eval("Name")+","+Eval("DESCRIPTION") %>' OnClick="btnEdit_Click" />
</ItemTemplate>
in onclick
protected void Btn_Delete_Click(object sender, EventArgs e)
{
string strName=((LinkButton)sender).CommandArgument.Split(',')[0];
string strDescription=((LinkButton)sender).CommandArgument.Split(',')[1];
}
protected void btnEdit_Click_Click(object sender, EventArgs e)
{
string strName=((LinkButton)sender).CommandArgument.Split(',')[0];
string strDescription=((LinkButton)sender).CommandArgument.Split(',')[1];
}

how to update a row through a row button click

i have a button in my gridview with the ID "btnApprove". what i want is when the user clicks the button, the row "Status" will be updated to 'Approved'. how can i acheive that? one row will only be updated when the button is clicked depending on the transaction number
here is my aspx code.
<asp:UpdatePanel ID="panel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TransactionID" OnRowDataBound="GridView1_OnRowDataBound" OnRowCommand="GridView1_RowCommand" CellPadding="4" AllowPaging="true" PageIndex="2" OnPageIndexChanging="GridView1_PageIndexChanging" HeaderStyle-BackColor ="CornflowerBlue" BorderWidth="1" BorderColor="Gray" Width="100%" CssClass=" table table-hover" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<img style="cursor:pointer" src ="../Images/Icons/plus2.png" />
<asp:Panel ID ="pnlDetails" runat="server" Style="display: none">
<asp:GridView ID="gvDet" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<Columns>
<%--<asp:BoundField ItemStyle-Width="20px" DataField="ID" HeaderText="ID" />--%>
<asp:BoundField ItemStyle-Width="200px" DataField="ItemType" HeaderText="Type" />
<asp:BoundField ItemStyle-Width="250px" DataField="ItemModel" HeaderText="Model" />
<asp:BoundField ItemStyle-Width="140px" DataField="ItemQuantity" HeaderText="Requested Quantity" />
<asp:BoundField ItemStyle-Width="80px" DataField="ItemUnit" HeaderText="Unit" />
<asp:BoundField ItemStyle-Width="100px" DataField="ItemDate" HeaderText="Date Needed" />
<asp:BoundField ItemStyle-Width="200px" DataField="ItemDesc" HeaderText="Description" />
<%--<asp:BoundField ItemStyle-Width="80px" DataField="ItemStatus" HeaderText="Status" />--%>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="TransactionID" HeaderText="Transaction Number" />
<asp:BoundField ItemStyle-Width="150px" DataField="DateFiled" HeaderText ="Date Filed" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqName" HeaderText="Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqCompany" HeaderText="Company" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqBranch" HeaderText="Branch" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqBU" HeaderText="Business Unit" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqDept" HeaderText="Department" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqSection" HeaderText="Section" />
<asp:BoundField ItemStyle-Width="150px" DataField="TransStatus" HeaderText="Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="btnApprove_Click" CssClass="btn btn-primary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="CornflowerBlue" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
Update here is my data source of my GridView
public void showTable()
{
Utility u = new Utility();
string conn = u.connect();
SqlConnection connUser = new SqlConnection(conn);
SqlDataAdapter adp = new SqlDataAdapter("select * from MosefTransaction where TransStatus = 'Pending'", connUser);
DataTable dt = new DataTable();
connUser.Open();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Add CommandArgument='<%# Container.DataItemIndex %>' to your button and in your code behind you can get the row that raised the event on Gridview's RowCommand event
<asp:Button ID="btnApprove" runat="server" Text="Approve" CommandName="ApproveTransaction" CommandArgument='<%# Container.DataItemIndex %>'/>
In your codebehind subscribe to gridview's row command event.
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ApproveTransaction")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvInfo.Rows[index];
string cellText = row.Cells[2].Text;
//Update your data in database here and rebind the gridview to updated data
}
}

Get Particular Grid Column value on button click?

MY grid view code
<asp:GridView ID="grdAccidentMaster" runat="server" AutoGenerateColumns="False"
Width="80%" BackColor="White" BorderColor="#DEDFDE" BorderStyle="Solid" BorderWidth="1px"
EmptyDataText="No Records found" CellPadding="4" ForeColor="Black" GridLines="Both"
DataKeyNames="IncidentNo" OnRowDataBound="grdAccidentMaster_RowDataBound">
<Columns>
<asp:BoundField DataField="IncidentNo" HeaderText="IncidentNo" />
<asp:BoundField DataField="CreatedDate" HeaderText="Created Date" />
<asp:BoundField DataField="AccidentDate" HeaderText="Accident Date" />
<asp:BoundField DataField="PoliceStation" HeaderText="Police Station" />
<asp:BoundField DataField="City" HeaderText="City Name" />
<asp:BoundField DataField="RoadCondition" HeaderText="Road Condition" />
<asp:BoundField DataField="RoadFeature" HeaderText="Road Feature" />
<asp:TemplateField HeaderText="Print">
<ItemTemplate>
<asp:Button runat="server" Text="Click" ID="btnClick" OnClick="btnPrint_click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
MY grid view Output
If i click the Click button under Print column. I need to get the IncidentNo value '4022' on onclick function.
Add an event in Gridview :
OnRowCommand="grdAccidentMaster_OnRowCommand"
Change template for button :
<ItemTemplate>
<asp:Button runat="server" Text="Click" ID="btnClick"
CommandName="SendIncidentNo" CommandArgument='<%# Eval("IncidentNo") %>' />
</ItemTemplate>
Code behind :
protected void grdAccidentMaster_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "SendIncidentNo") return;
int incidentNo = Convert.ToInt32(e.CommandArgument);
// do something
}

Delete and Update Row from GridView on Image Button

I want to delete the records from the Gridview on the click of ImageButton. Please see the GridView Code for your reference:-
<asp:GridView ID="grdCSRPageData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3" AutoGenerateColumns="false" OnDataBound="grdCSRPageData_DataBound" PageSize="5" AllowPaging="true" OnPageIndexChanging="grdCSRPageData_PageIndexChanging" OnRowCommand="grdCSRPageData_RowCommand">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField ItemStyle-Width="30">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="page_title" HeaderText="Page Title" ItemStyle-Width="30" />
<asp:BoundField DataField="page_description" HeaderText="Page Description" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_title" HeaderText="Meta Title" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_keywords" HeaderText="Meta Keywords" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_description" HeaderText="Meta Description" ItemStyle-Width="30" />
<asp:BoundField DataField="Active" HeaderText="Active" ItemStyle-Width="30" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/images/edit.png" Width="15" Height="15" />
<span onclick="return confirm('Are you sure want to delete?')">
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="DeleteRow" />
</span>
</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>
</Columns>
</asp:GridView>
Tried with 'RowCommand' property of GridView and stucked how to delete the Row
See the code:-
protected void grdCSRPageData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DeleteRow")
{
//incase you need the row index
int rowIndex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
int Id = Convert.ToInt32(e.CommandArgument);
//followed by your code
}
}
try this
protected void grdCSRPageData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DeleteRow")
{
int rowIndex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
grdCSRPageData.Rows.RemoveAt(rowIndex);
//delete from database
using (SqlConnection connection = new SqlConnection(connectionString))
{
int Id = Convert.ToInt32(e.CommandArgument);
string sql = "DELETE FROM YourTable WHERE ID = #ID";
SqlCommand cmd = new SqlCommand(sql,connection);
cmd.Parameters.AddWithValue("#ID",Id);
cmd.ExecuteNonQuery();
}
}
}
refer here for documentation about GridView remove.
SqlCommand documentation
update
to get the gridView from its row you can call this function
public static GridView GetParentGridView(GridViewRow row)
{
GridView gridView = (GridView)row.NamingContainer;
return gridView;
}
use it to get the gridview in my code
replace
grdCSRPageData.Rows.RemoveAt(rowIndex)
with
GetParentGridView((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).Rows.RemoveAt(rowIndex)
be sure also to bind the grid only if there is no postback on page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind the gridView
}
}
Tried with Something like this and it worked.:-
<asp:GridView ID="grdCSRPageData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3" AutoGenerateColumns="False" OnDataBound="grdCSRPageData_DataBound" AllowPaging="True" OnPageIndexChanging="grdCSRPageData_PageIndexChanging" DataKeyNames="Id" OnRowDeleting="grdCSRPageData_RowDeleting" OnClientClick="return confirm('Are you sure you want to delete this record?')">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<%-- <asp:TemplateField ItemStyle-Width="30">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>--%>
<asp:BoundField DataField="page_title" HeaderText="Page Title" ItemStyle-Width="30" />
<asp:BoundField DataField="page_description" HeaderText="Page Description" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_title" HeaderText="Meta Title" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_keywords" HeaderText="Meta Keywords" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_description" HeaderText="Meta Description" ItemStyle-Width="30" />
<asp:BoundField DataField="Active" HeaderText="Active" ItemStyle-Width="30" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%">
<ItemTemplate>
<%-- <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" Text="Edit" />--%>
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/images/edit.png" Width="15" Height="15" />
<%-- <span onclick="return confirm('Are you sure want to delete?')"> -- %>
<%--<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" ></asp:LinkButton>--%>
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</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>
</Columns>
</asp:GridView>
Also see the code behind:-
protected void grdCSRPageData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
bool IsDeleted = false;
//getting key value, row id
int Id = Convert.ToInt32(grdCSRPageData.DataKeys[e.RowIndex].Value.ToString());
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "DELETE FROM tbl_Pages WHERE Id=#ID";
cmd.Parameters.AddWithValue("#ID", Id);
cmd.Connection = conn;
conn.Open();
IsDeleted = cmd.ExecuteNonQuery() > 0;
conn.Close();
}
}
if (IsDeleted)
{
//record has been deleted successfully!
//call here gridview bind method and replace it..
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Page Succesfully deleted');window.location ='csrpage.aspx';", true); ;
grdCSRPageData.DataBind();
}
else
{
//Error while deleting record
Response.Write("Some error");
}
}
Also the binding of the data should be inside the '(!IsPostBack)' method

c# gridview onrowupdating event page does not render

I am having an issue. I have a page that has a Gridview with edit/update/cancel edit on it.
The page works/renders until I add the OnRowUpdating="UpdateRow" to the Gridview. Then the page does not render at all.
In Debugging it does not even get to the page load events. When I take this property out of the Gridview (without touching any code behind code) the page renders. I have been googling and can not find anything similar. I can find if it does not fire/does not work but nothing about page not rendering.
My code is below:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="false"
Width="100%"
OnRowEditing="EditRow"
OnRowUpdating="UpdateRow"
OnRowCancelingEdit="CancelEditRow"
DataKeyNames="OnCallID"
AllowPaging="true"
PageSize="50"
OnPageIndexChanging="ChangePage"
OnRowDataBound="gvVehicle_OnRowDataBound"
>
<Columns>
<asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="25px">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="OnCallID" DataField="OnCallID" ReadOnly="true" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="25px" Visible="false" />
<asp:TemplateField HeaderText="OnCallID" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="600px" Visible="false" >
<ItemTemplate><%# Eval("OnCallID")%></ItemTemplate>
<EditItemTemplate>
<asp:Label ID="HDNOnCallID" runat="server" Visible="true" Text='<%# Eval("OnCallID")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Team Member Name" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="600px" >
<ItemTemplate><%# Eval("TeamMemberName")%></ItemTemplate>
<EditItemTemplate>
<asp:Label ID="LBLUserIDLST" runat="server" Visible="true" Text='<%# Eval("TeamMemberName")%>' />
<!--<asp:ListBox ID="UserIDLST" runat="server" SelectionMode="Single" Visible="true" Width="100" Rows="1"></asp:ListBox> -->
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="On Call Start Date" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="600px" >
<ItemTemplate><%# Eval("OnCallStartDate")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="StartDateEditUpdate" runat="server" MaxLength="10" name="StartDateEditUpdate" Width="70" Text='<%# Eval("OnCallStartDate")%>'></asp:TextBox>
<asp:Image id="StartDateEditUpdateCalImg" runat="server" ImageUrl="../Images/CalendarImg.png" ImageAlign="Bottom" />
<ajaxToolkit:CalendarExtender ID="extendStartDateEditUpdate" runat="server" CssClass="datePicker" PopupPosition="TopLeft"
TargetControlID="StartDateEditUpdate" Format="MM/dd/yyyy" PopupButtonID="StartDateEditUpdateCalImg">
</ajaxToolkit:CalendarExtender>
<asp:RequiredFieldValidator ID="validateStartDateEditUpdate"
ControlToValidate="StartDateEditUpdate"
Text="You must Type Something in for Start Date!"
runat="server"
Enabled="true"
CssClass="validatorErrorDisplay"
/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="On Call Start Time" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="600px" >
<ItemTemplate><%# Eval("OnCallStartTime")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="StartTimeEditUpdate" runat="server" MaxLength="10" Width="100" Text='<%# Eval("OnCallStartTime")%>'></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="MaskedEditorStartTimeEditUpdate" runat="server"
TargetControlID="StartTimeEditUpdate"
Mask="99:99"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Time"
InputDirection="LeftToRight"
ErrorTooltipEnabled="True"
AcceptAMPM="true"
AutoComplete="true"
/>
<ajaxToolkit:MaskedEditValidator ID="MaskedEditorValidatorStartTimeEditUpdate" runat="server"
ControlExtender="MaskedEditorStartTimeEditUpdate"
ControlToValidate="StartTimeEditUpdate"
IsValidEmpty="true"
AcceptAMPM = "true"
InvalidValueMessage="This is not a valid time2"
InvalidValueBlurredMessage="This is not a valid time"
Enabled="true"
TooltipMessage="Must be in Time format HH:MM AM/PM"
ValidationExpression="(0[1-9]|1[0-2]):([0-5][0-9]) (AM|PM|am|pm)"
/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="On Call End Date" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="600px" >
<ItemTemplate><%# Eval("OnCallEndDate")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="EndDateEditUpdate" runat="server" MaxLength="10" name="EndDateEditUpdate" Width="70" Text='<%# Eval("OnCallEndDate")%>'></asp:TextBox>
<asp:Image id="EndDateEditUpdateCalImg" runat="server" ImageUrl="../Images/CalendarImg.png" ImageAlign="Bottom" />
<ajaxToolkit:CalendarExtender ID="extendEditUpdate" runat="server" CssClass="datePicker" PopupPosition="TopLeft"
TargetControlID="EndDateEditUpdate" Format="MM/dd/yyyy" PopupButtonID="EndDateEditUpdateCalImg">
</ajaxToolkit:CalendarExtender>
<asp:RequiredFieldValidator ID="validateEndDateEditUpdate"
ControlToValidate="EndDateEditUpdate"
Text="You must Type Something in for End Date!"
runat="server"
Enabled="true"
CssClass="validatorErrorDisplay"
/>
<asp:CompareValidator ID="CompareStartDateEndDateEditUpdateValidator" Operator="GreaterThanEqual" Type="Date"
ControlToValidate="EndDateEditUpdate" ControlToCompare="StartDateEditUpdate"
ErrorMessage="End Date must be greater then Start Date!" runat="server" BackColor="Red" ForeColor="White" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="On Call End Time" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="600px" >
<ItemTemplate><%# Eval("OnCallEndTime")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="EndTimeEditUpdate" runat="server" MaxLength="10" Width="100" Text='<%# Eval("OnCallEndTime")%>'></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="MaskedEditorEndTimeEditUpdate" runat="server"
TargetControlID="EndTimeEditUpdate"
Mask="99:99"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Time"
InputDirection="LeftToRight"
ErrorTooltipEnabled="True"
AcceptAMPM="true"
AutoComplete="true"
/>
<ajaxToolkit:MaskedEditValidator ID="MaskedEditorValidatorEndTimeEditUpdate" runat="server"
ControlExtender="MaskedEditorEndTimeEditUpdate"
ControlToValidate="EndTimeEditUpdate"
IsValidEmpty="true"
AcceptAMPM = "true"
InvalidValueMessage="This is not a valid time2"
InvalidValueBlurredMessage="This is not a valid time"
Enabled="true"
TooltipMessage="Must be in Time format HH:MM AM/PM"
ValidationExpression="(0[1-9]|1[0-2]):([0-5][0-9]) (AM|PM|am|pm)"
/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#efefef" />
<FooterStyle BackColor="#F37721" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#F37721" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
protected void EditRow(object sender, GridViewEditEventArgs e)
{
// this enables/disables the add new row validation
EnableDisableValidationsAndButtons(false);
GridView1.EditIndex = e.NewEditIndex;
this.PopulateData();
}
protected void CancelEditRow(object sender, GridViewCancelEditEventArgs e)
{
// this enables/disables the add new row validation
EnableDisableValidationsAndButtons(true);
GridView1.EditIndex = -1;
this.PopulateData();
}
protected void UpdateRow(object sender, GridViewUpdatedEventArgs e)
{
//GridViewRow row = (GridViewRow)GridView1.Rows[e.NewEditIndex];
GridViewRow row = (GridViewRow)GridView1.Rows[GridView1.EditIndex];
Label txtOnCallID = (Label)row.FindControl("HDNOnCallID");
String OnCallID = txtOnCallID.Text.ToString();
TextBox txtBoxStartDate = (TextBox)row.FindControl("StartDate");
String StartDate = txtBoxStartDate.Text.ToString();
TextBox txtBoxStartTime = (TextBox)row.FindControl("StartTime");
String StartTime = txtBoxStartTime.Text.ToString();
TextBox txtBoxEndDate = (TextBox)row.FindControl("EndDate");
String EndDate = txtBoxStartDate.Text.ToString();
TextBox txtBoxEndTime = (TextBox)row.FindControl("EndTime");
String EndTime = txtBoxEndTime.Text.ToString();
// do update stuff here
SQL CODE HERE
// run update
//new DatabaseConnection().RSExecute(sCommand);
lblMessage.Text = "Record successfully Updated!";
GridView1.EditIndex = -1;
//this.PopulateData();
// doing a redirect here for the full URL because if you refresh page it adds the record duplicate times
// also some of the form fields are still filled out, this removes them as well
Response.Redirect(Request.Url.AbsoluteUri);
}
Change the signature of the function as below and check. You are binding function with GridViewUpdatedEventArgs instead of GridViewUpdateEventArgs
protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
}

Categories