How to validate time using compare validator? - asp.net

How to validate time using compare validator?
I have a Editable gridview that contain 2 column for time Start & End Time and I want to validate them that star time must be earlier than end time. I have 24 hour format.
For Example If I entered Start Time: 20:30 and the End Time is 19:30 it should give me error and prevent me from Updating the row

simply you can try this
In aspx code
<div>
<asp:GridView ID="gvDetails" DataKeyNames="EventID,EventName" runat="server" AutoGenerateColumns="false"
CssClass="Gridview" HeaderStyle-BackColor="#61A6F8" ShowFooter="true" HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White" OnRowCancelingEdit="gvDetails_RowCancelingEdit"
OnRowDeleting="gvDetails_RowDeleting" OnRowEditing="gvDetails_RowEditing" OnRowUpdating="gvDetails_RowUpdating"
OnRowCommand="gvDetails_RowCommand">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg"
ToolTip="Update" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg"
ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg"
ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server"
ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg"
CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EventName">
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("EventName") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("EventName") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrusrname" runat="server" />
<asp:RequiredFieldValidator ID="rfvEventName" runat="server" ControlToValidate="txtftrusrname"
Text="*" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StartTime">
<EditItemTemplate>
<asp:TextBox ID="txtStartTime" runat="server" Text='<%#Eval("StartTime") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStartTime" runat="server" Text='<%#Eval("StartTime") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrStartTime" runat="server" />
<asp:RequiredFieldValidator ID="rfvStartTime" runat="server" ControlToValidate="txtftrStartTime"
Text="*" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EndTime">
<EditItemTemplate>
<asp:TextBox ID="txtstate" runat="server" Text='<%#Eval("EndTime") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblstate" runat="server" Text='<%#Eval("EndTime") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrEndTime" runat="server" />
<asp:RequiredFieldValidator ID="rfvEndTime" runat="server" ControlToValidate="txtftrEndTime"
Text="*" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
And at code behind
protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
TextBox txtStartTime = (TextBox)gvDetails.FooterRow.FindControl("txtftrStartTime");
TextBox txtEndTime = (TextBox)gvDetails.FooterRow.FindControl("txtftrEndTime");
if (Convert.ToDateTime(txtEndTime.Text.Trim().ToString()) <= Convert.ToDateTime(txtStartTime.Text.Trim().ToString()))
{
lblresult.Text = "Please enter valid end time";
}
else
{
con.Open();
SqlCommand cmd =
new SqlCommand(
"insert into tblEvents(EventName,StartTime,EndTime) values('" + txtUsrname.Text + "','" +
txtStartTime.Text + "','" + txtEndTime.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
lblresult.ForeColor = Color.Green;
lblresult.Text = txtUsrname.Text + " Details inserted successfully";
}
else
{
lblresult.ForeColor = Color.Red;
lblresult.Text = txtUsrname.Text + " Details not inserted";
}
}
}
}

Related

GridView not showing up in ASP.NET?

For some reason my gridview is not showing up. On a debug I get rows and columns from the database when I check Results. I'm using a view to get the fields I need. I tried setting AutoGenerateColumns to true, but no luck. Am I missing something?
My aspx page:
<asp:Content ID="ContentMain" ContentPlaceHolderID="cphMain" runat="Server">
<asp:Panel ClientIDMode="Static" ID="meritData" runat="server" Visible="false" CssClass="dataTable">
<h5>Faculty Teaching Assignments</h5>
<p id="pMeritMsg" runat="server" class="userMsg"></p>
<asp:GridView ID="gridFTA" runat="server" AutoGenerateColumns="false"
ShowHeaderWhenEmpty="true"
ShowFooter="true"
DataKeyNames="FacultyId">
<Columns>
<asp:TemplateField HeaderText="Faculty Name">
<ItemTemplate>
<asp:Label ID="txtFacultyName" runat="server" Text='<%#Bind("FacultyName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblFacultyName" runat="server" width="40px" Text='<%#Bind("FacultyName") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inFacultyName" width="140px" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number Of Courses">
<ItemTemplate>
<asp:Label ID="lblNumberOfCourses" runat="server" Text='<%#Bind("NumberOfCourses") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNumberOfCourses" width="70px" runat="server" Text='<%#Bind("NumberOfCourses") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inNumberOfCourses" width="120px" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Adjustment">
<ItemTemplate>
<asp:Label ID="lblAdjustment" runat="server" Text='<%#Bind("Adjustment") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblAdjustment" runat="server" visible = "false" Text='<%#Bind("Adjustment") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inAdjustment" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reason">
<ItemTemplate>
<asp:Label ID="lblReason" runat="server" Text='<%#Bind("Reason") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblReason" runat="server" visible = "false" Text='<%#Bind("Reason") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inReason" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course">
<ItemTemplate>
<asp:Label ID="lblCourse" runat="server" Text='<%#Bind("CourseNumber") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblCourse" runat="server" visible = "false" Text='<%#Bind("CourseNumber") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inCourse" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" CommandName="AddNew" Text="Add New Faculty Teaching Assignment" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</asp:Content>
My code behind:
protected clsData moData = new clsData();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpContext.Current.Session["FTA"] = "";
BindData("");
}
}
protected void BindData(String FTA)
{
DataTable Results = moData.GetFTA(FTA);
gridFTA.DataSource = Results;
gridFTA.DataBind();
}
Code from the Database
public DataTable GetFTA(
string FTA)
{
// Prepare connection
SqlConnection conn = new SqlConnection(msConnString);
// Prepare command
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
if (FTA == "")
{ cmd.CommandText = "Select * from View_FTA order by FacultyName"; }
DataTable dt = new DataTable();
try
{
conn.Open();
dt.Load(cmd.ExecuteReader());
}
catch (Exception ex)
{
logException(ex);
}
finally
{
conn.Close();
}
return dt;
}
Your asp:Panel has Visible set to false. This makes the ASP.NET engine to not to render the control (and its contents) at all.

export gridview to excel is not working with Ajax and Update panel

I have a grid-view in Update panel. there is a link button inside grid-view which fire export command in row command event. There is an another grid-view which get exported on this command code. I debug, the pointers goes in but the export not happening. I know i am missing something here, but not able to get it after spending lot of hours. The Script manager is on master page.
Can any one help please. Below is the code.
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="grd_Report" runat="server" ShowFooter="true" CssClass="ui-jqgrid-btable table-hover" AutoGenerateColumns="false" AllowPaging ="true" PageSize ="12" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_CampaignName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreateDate">
<ItemTemplate>
<asp:Label ID="lbl_CreateDate" runat="server" Text='<%#Eval("CreateDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="130px" />
<HeaderStyle Width="130px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="sender">
<ItemTemplate>
<asp:Label ID="lbl_senderID" runat="server" Text='<%#Eval("sender") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="85px" />
<HeaderStyle Width="85px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:Label ID="lbl_text" runat="server" Text='<%#Eval("Text") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList class="ui-pg-selbox" ID="drp_itemCount" runat="server" AutoPostBack="true"
onselectedindexchanged="drp_itemCount_SelectedIndexChanged" >
<asp:listItem>10</asp:listItem>
<asp:listItem>15</asp:listItem>
<asp:listItem>50</asp:listItem>
<asp:listItem>100</asp:listItem>
<asp:listItem>200</asp:listItem>
</asp:DropDownList>
</FooterTemplate>
<ItemStyle Width="375px" />
<HeaderStyle Width="375px" />
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:Label ID="lbl_SMSCount" runat="server" Text='<%#Eval("Count") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="80px" />
<HeaderStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delivery Count">
<ItemTemplate>
<table class="deliveryCount">
<tr>
<td style="padding:5px;width:180px" colspan="2">Total: <%#Eval("Total")%></td>
</tr>
<table style="font-size:11px;width:180px;">
<tr>
<td style="padding:5px;font-size:11px;width:80px">A: <%#Eval("A")%></td>
<td style="padding:5px;font-size:11px;width:80px">B: <%#Eval("B")%></td>
</tr
<tr>
<td style="padding:5px;font-size:11px;width:80px">2: <%#Eval("2")%></td>
<td style="padding:5px;font-size:11px;width:80px">1: <%#Eval("1")%></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnk_download" style="font-size:11px;" CommandName="download" CommandArgument='<%#Eval("CampaignId") %>' text="Download" class="ui-icon-document"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" Font-Size="13" Font-Bold="true" />
<RowStyle Font-Names="Helvetica" Font-Size="10" />
<PagerStyle CssClass ="gridviewPager" Height="30px" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="grd_export" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel6" runat="Server">
<ContentTemplate>
<asp:GridView ID="grd_export" runat="server" Visible="false" AutoGenerateColumns ="false" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number">
<ItemTemplate>
<asp:Label ID="lbl_Number" runat="server" Text='<%#Eval("Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lbl_Id" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:Label ID="lbl_text" runat="server" Text='<%#Eval("Text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="text Id">
<ItemTemplate>
<asp:Label ID="lbl_MessageId" runat="server" Text='<%#Eval("textId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lbl_Date" runat="server" Text='<%#Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lbl_Status" runat="server" Text='<%#Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credits">
<ItemTemplate>
<asp:Label ID="lbl_Credits" runat="server" Text='<%#Eval("Credits") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Below is the code on Row Command event
If e.CommandName = "download" Then
Dim str As String
str = " select * FROM [Box] where MsgId='" + e.CommandArgument + "'"
Dim com As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(com)
Dim dt As New DataTable
da.Fill(dt)
If Not dt Is Nothing Then
If dt.Rows.Count > 0 Then
grd_export.DataSource = dt
grd_export.DataBind()
grd_export.Visible = True
Response.Clear()
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.AddHeader("content-disposition", "attachment;filename=Message.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.xls"
Me.EnableViewState = False
Dim stringwrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlwrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringwrite)
grd_export.RenderControl(htmlwrite)
Response.Write(stringwrite.ToString())
grd_export.Visible = False
Response.End()
End If
End If
End If
You need to add a postback trigger to your update panel for respose.write to work.

Selected row to textbox when clicked on row not firing

I can click on row to selected them but when i need them to textbox it's not firing i tried to use Autopostback on textbox but it's doesn't work i think it's because of for each that not make row.cells(0) work
i write a code like this
VB.NET
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
If row.RowIndex = GridView1.SelectedIndex Then
row.BackColor = ColorTranslator.FromHtml("#E294FF")
txtrepname.Text = row.Cells(0).Text
row.ToolTip = String.Empty
Else
row.BackColor = ColorTranslator.FromHtml("#FFFFFF")
row.ToolTip = "Click to select this row."
End If
Next
End Sub
Gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" ShowFooter="True" Width="1280px" OnRowDataBound = "OnRowDataBound" OnSelectedIndexChanged = "OnSelectedIndexChanged"
onrowupdating="GridView1_RowUpdating" DataKeyNames="REP_NAME" PageSize="9999">
<Columns>
<asp:TemplateField HeaderText="" >
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="" CommandName="Edit" ToolTip="Edit"
CommandArgument=''><img src="Images/ad_edit.png" BORDER="0" /></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
ToolTip="Delete" OnClientClick='return confirm("Are you sure you want to delete this entry?");'
CommandArgument='' BorderColor="#CC3300"><img src="Images/ad_delete.png" BORDER="0" /></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="editGrp" CommandName="Update" ToolTip="Save"
CommandArgument=''><img src="Images/ad_save.png" BORDER="0" /></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="Cancel" ToolTip="Cancel"
CommandArgument=''><img src="Images/ad_reload.png" BORDER="0" /></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="newGrp" CommandName="InsertNew" ToolTip="Add New Entry"
CommandArgument=''><img src="Images/ad_add.png" BORDER="0"/></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="CancelNew" ToolTip="Cancel"
CommandArgument=''><img src="Images/ad_reload.png" BORDER="0"/></asp:LinkButton>
</FooterTemplate>
<HeaderStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Report Name" SortExpression="REP_NAME" >
<ItemTemplate>
<div style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap; min-width:150px; ">
<asp:Label ID="Label1" runat="server" Text='<%# Bind("REP_NAME") %>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("REP_NAME") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtREP_NAMENew" runat="server" CssClass="" Width="60px" ></asp:TextBox>
<asp:RequiredFieldValidator ID="valREP_FILEID" runat="server" ControlToValidate="txtREP_NAMENew"
Display="Dynamic" ErrorMessage="Province is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
<HeaderStyle Width="80px" />
</asp:TemplateField>
</Columns>
</asp:GridView>

hide row on clicking delete link in gridview

I have a gridview, in which two link buttons are there.
Here it is:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="5" OnRowCommand="gv_RowCommand"
AllowPaging="true" DataKeyNames="ID" CssClass="mGrid" BackColor="White" BorderColor="Silver" BorderStyle="Double"
BorderWidth="1px" CellPadding="4" >
<RowStyle BackColor="White" Width="150%" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false" >
<ItemTemplate>
<asp:Label ID="lblID" Text='<%#Bind("ID") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Version">
<ItemTemplate>
<asp:Label ID="lblversion" Text='<%#Bind("version") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image ID">
<ItemTemplate>
<asp:Label ID="lblimageid" runat="server" Text='<%#Bind("image_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Getty ID">
<ItemTemplate>
<asp:Label ID="lblgettyid" runat="server" Text='<%#Bind("getty_id") %>'></asp:Label>
</ItemTemplate>
<%--<EditItemTemplate>
<asp:TextBox ID="txtgettyid" Text='<%#Bind("getty_id") %>' runat="server"></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pool Letter">
<ItemTemplate>
<asp:Label ID="lblpoolletter" runat="server" Text='<%#Bind("pool_letter") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Use">
<ItemTemplate>
<asp:Label ID="lbltouse" runat="server" Text='<%#Bind("to_use") %>'></asp:Label>
</ItemTemplate>
<%-- <EditItemTemplate>
<asp:TextBox ID="txttouse" Text='<%#Bind("to_use") %>' runat="server"></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Clue">
<ItemTemplate>
<asp:Label ID="lblclue" runat="server" Text='<%#Bind("clue") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Range">
<ItemTemplate>
<asp:Label ID="lblrange" runat="server" Text='<%#Bind("range") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="last_updated">
<ItemTemplate>
<asp:Label ID="lbllastupdated" runat="server" Text='<%#Bind("last_updated") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<%#Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First">
<ItemTemplate>
<asp:Label ID="lblfirst" runat="server" Text='<%#Bind("first") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle">
<ItemTemplate>
<asp:Label ID="lblmiddle" runat="server" Text='<%#Bind("middle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last">
<ItemTemplate>
<asp:Label ID="lbllast" runat="server" Text='<%#Bind("last") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemStyle Wrap="false" Width="100px" />
<ItemTemplate>
<asp:LinkButton ID="lnkbtnedit" Text="Edit" Visible="true" style="color:#006699;" CommandName="Edit" runat="server"></asp:LinkButton>
<asp:LinkButton ID="lnkbtndel" Text="Delete" Visible="true" style="color:#006699;" CommandName="Delete" runat="server"> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#CCFFFF" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
con = new SqlConnection(conString);
con.Open();
int v = version + 1 ;
string updatedetails = "update tblfameface set status ='Deleted',version=" + v + " where ID=" + ID + " ";
SqlCommand cmd = new SqlCommand(updatedetails, con);
int temp = cmd.ExecuteNonQuery();
con.Close();
//int rows = GridView1.CurrentCell.RowIndex;
//GridView1.Rows.RemoveAt(rows);
bindDB();
LinkButton btndel = (LinkButton)GridView1.Rows[row.RowIndex].FindControl("lnkbtndel");
btndel.Visible = false;
}
On clicking Edit link,it is working as desired and
When I click on Delete link, I want that row to be hidden.
which event should be fired and how can it be handled?
how can I achieve this??
This is Aspx:
<Columns>
<asp:TemplateField HeaderText="Hide"> <ItemTemplate> <asp:LinkButton ID="lnkHide" runat="server" Text="Hide" OnClick="LnkHide_Click" /> </ItemTemplate> </asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
This is Code behind:
protected void LnkHide_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
clickedRow.Visible = false;
}
Try this code
private void button1_Click(object sender, EventArgs e)
{
int row = dataGridView1.CurrentCell.RowIndex;
dataGridView1.Rows.RemoveAt(row);
}
Try this code........

Search gridview ItemTemplate textbox value on buttonclick

My Scenario is that i had a gridview,i had taken itemtemplate textboxes and a button,when the gridview is loaded the user enters a ServiceCode based on the ServiceCode and on button click,data should be displayed into the gridview,when the data is displaye in the gridview the user enters the quantity and the discount then the result should be displayed in the netamount,i had written the code but it is not working i would appreciate if someone share thier knowledge with me.
My Axpx Code is:
<asp:GridView ID="GridView1" runat="server" Height="157px" Width="639px"
AutoGenerateColumns="False" BorderWidth="1px"
HorizontalAlign="Justify" onrowdatabound="GridView1_RowDataBound"
BackColor="LightGoldenrodYellow" BorderColor="Tan" CellPadding="2"
ForeColor="Black" GridLines="None" onrowcommand="GridView1_RowCommand">
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="#FFFFC4" Font-Bold="True" ForeColor="#BF6000"/>
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<asp:Label ID="label10" runat="server" Font-Bold="true" ForeColor="#BF6000" Text="<%# Container.DataItemIndex + 1 %>">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Serv Code" >
<ItemTemplate>
<asp:TextBox ID="TxtServiceCode" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.ServiceCode") %>'
runat="server" Width="55px">
</asp:TextBox>
<asp:Button ID="Insert" runat="server" BackColor="Ivory" CommandName="InsertRecord"
Font-Bold="True" ForeColor="#BF6000" Text="Insert" onclick="Insert_Click"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="TxtName" runat="server" ReadOnly="true" Font-Bold="true" ForeColor="#BF6000" Width="150px" Text='<%# DataBinder.Eval(Container, "DataItem.ServiceName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Serv Amt">
<ItemTemplate>
<asp:TextBox ID="TxtServiceAmount" ReadOnly="true" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.ServiceAmount") %>'
runat="server" Width="55px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="TxtQuantity" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.Quantity") %>'
runat="server" Width="55px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Disc Amt">
<ItemTemplate>
<asp:TextBox ID="TxtdiscAmt" Font-Bold="true" ForeColor="#BF6000" Text ='<%# DataBinder.Eval(Container,"DataItem.Discount") %>'
AutoPostBack="true" OnTextChanged="TxtdiscAmt_TextChanged" runat="server" Width="55px"> </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Net Amt">
<ItemTemplate>
<asp:TextBox ID="lblNet" runat="Server" Font-Bold="true" ForeColor="#BF6000" Width="55" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
MY CS Code is:
DataSet dss = new DataSet();
SqlConnection MyConnection = new SqlConnection("server=prog;database=mydatabase; UID=sa;PWD=nato123;");
SqlCommand sqlcmd = new SqlCommand("select * from [Bill]", MyConnection);
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
Instead of using the button OnClick event, I would use OnCommand and pass the item index in the CommandArgument:
<asp:Button ID="Button1" runat="server" OnCommand="Button1_Command" CommandArgument='<%#Container.ItemIndex%>' />
Code-behind:
protected void Button1_Command(object sender, CommandEventArgs e)
{
GridViewRow row = GridView1.Rows[(int)e.CommandArgument];
if (row != null)
{
TextBox txt = row.FindControl("TextBox1") as TextBox;
if (txt != null)
{
string value = txt.Text;
}
}
}

Resources