this is my gridview in .NET c# with pagination
<asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="ko" EnableViewState="true"
runat="server" DataKeyNames="ID" CssClass="mGrid" HorizontalAlign="Center"
OnRowDataBound="gvProducts_RowDataBound"
AllowPaging="True" PageSize="10"
OnPageIndexChanging="OnPageIndexChanging">
<AlternatingRowStyle CssClass="altrows" />
<Columns>
<asp:BoundField DataField="Nr" HeaderText="Nr." ReadOnly="true" HtmlEncode="false"
ItemStyle-CssClass="ddl_Class_new" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlstatus" runat="server" CssClass="pure-u-23-24"
AutoPostBack="true" OnSelectedIndexChanged="ddlstatus_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/aspnet/img/bot_back_1.gif"
CommandArgument="First" CommandName="Page" />
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/aspnet/img/bot_back.gif"
CommandArgument="Prev" CommandName="Page" />
Page
<asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class"
OnSelectedIndexChanged="ddlPages_SelectedIndexChanged">
</asp:DropDownList>
of
<asp:Label ID="lblPageCount" runat="server"></asp:Label>
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="/aspnet/img/bot_next.gif"
CommandArgument="Next" CommandName="Page" />
<asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/aspnet/img/bot_next_1.gif"
CommandArgument="Last" CommandName="Page" />
</PagerTemplate>
</asp:GridView>
on this gridview i have added this dropdownlist ddlstatus
<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlstatus" runat="server" CssClass="pure-u-23-24"
AutoPostBack="true" OnSelectedIndexChanged="ddlstatus_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
when the dropdownlist ddlstatus is changed i need open new webpage on the browser
protected void ddlstatus_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ebb = (DropDownList)sender;
GridViewRow gvr = (GridViewRow)ebb.NamingContainer;
string dvalue = gvr.Cells[1].Text;
ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('newpage.aspx?ID=" + dvalue.ToString() + "');", true);
}
but the new webpage on the browser is opened also if try change page on gridview
protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvrPager = gvProducts.BottomPagerRow;
DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
gvProducts.PageIndex = ddlPages.SelectedIndex;
BindData();
}
protected void Paginate(object sender, CommandEventArgs e)
{
int intCurIndex = gvProducts.PageIndex;
switch (e.CommandArgument.ToString().ToLower())
{
case "First":
gvProducts.PageIndex = 0;
break;
case "Prev":
gvProducts.PageIndex = intCurIndex - 1;
break;
case "Next":
gvProducts.PageIndex = intCurIndex + 1;
break;
case "Last":
gvProducts.PageIndex = gvProducts.PageCount - 1;
break;
}
gvProducts.DataBind();
}
protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvProducts.PageIndex = e.NewPageIndex;
BindData();
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvProducts.PageIndex = e.NewPageIndex;
BindData();
}
this is the the RowDataBound event
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
if (lblPageCount != null)
lblPageCount.Text = gvProducts.PageCount.ToString();
for (int i = 1; i <= gvProducts.PageCount; i++)
{
ddl.Items.Add(i.ToString());
}
ddl.SelectedIndex = gvProducts.PageIndex;
if (gvProducts.PageIndex == 0)
{
((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
}
if (gvProducts.PageIndex + 1 == gvProducts.PageCount)
{
((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
}
}
}
i don't understand why this happens?
how to do resolve this?
thanks in advance for any help
I am trying to export Grid data to csv file in asp.net i exported successfully but Audio File name column data is not displaying in downloaded file (it is button in TemplateField tag i need to print button text in csv file ) ,anything i forget to add ? How to fix this issue ? Thank you
.aspx code
<asp:GridView ID="GridView1" Font-Size="Medium" CssClass="mGrid" runat="server"
HeaderStyle-CssClass="header-center" PageSize="15"
AutoGenerateColumns="false" AllowPaging="True" AllowSorting="True"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<%--<asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-ForeColor="White" />--%>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("Id")%>' CommandArgument='<%#Eval("Id")%>' Visible="false">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Vendor Name" />
<asp:BoundField DataField="AudFileSize" HeaderText="AudFileSize" />
<asp:BoundField DataField="AudFileDuration" HeaderText="AudFileDuration" />
<%-- <asp:TemplateField HeaderText="Audio FileName">
<ItemTemplate>
<asp:Button ID="btnAudioFileName"
Text='<%#System.IO.Path.GetFileNameWithoutExtension(Eval("AudioFileName").ToString())%>'
runat="server" CommandArgument='<%#Eval("AudioFileName")%>'
class="btn btn-link" CommandName="AudioFileName" />
<%-- <sup><asp:Label ID ="lblrepetedCount" Text='<%#Eval("Reject")%>' runat="server"
CommandArgument='<%#Eval("Reject")%>' CommandName="Reject"></asp:Label></sup>--%>
<%-- </ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Audio FileName">
<ItemTemplate>
<asp:Button ID="btnAudioFileName"
Text='<%#System.IO.Path.GetFileNameWithoutExtension(Eval("AudioFileName").ToString())%>'
runat="server" CommandArgument='<%#Eval("AudioFileName")%>'
class="btn btn-link" CommandName="AudioFileName" />
<%-- <sup><asp:Label ID ="lblrepetedCount" Text='<%#Eval("Reject")%>' runat="server"
CommandArgument='<%#Eval("Reject")%>' CommandName="Reject"></asp:Label></sup>--%>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:BoundField DataField="FirstName" HeaderText="Physician Name" />--%>
<%--<asp:BoundField DataField="PhysicianID" HeaderText="PhysicianID" />--%>
<asp:BoundField DataField="MrnNo" HeaderText="MrnNo" NullDisplayText=" " />
<asp:BoundField DataField="PatientName" HeaderText="PatientName" NullDisplayText=" "/>
<asp:BoundField DataField="PhysicianName" HeaderText="PhysicianName" />
<asp:BoundField DataField="Reject" HeaderText="Reject" Visible="false" />
<%--<asp:BoundField DataField="BucketName" HeaderText="Bucket Name" Visible="false" />--%>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblBucketName" runat="server" Text='<%#Eval("BucketName")%>' CommandArgument='<%#Eval("BucketName")%>' Visible="false">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs code here
protected void btnExportToCSV_Click(object sender, EventArgs e)
{
ExportGridToCSV();
}
private void ExportGridToCSV()
{
//To Export all pages
GridView1.AllowPaging = false;
FillData();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Transcribeworkdetails.csv");
Response.Charset = "";
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.DataBind();
string[] hiddenCols = { "", "Reject" };
StringBuilder columnbind = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
bool isHidden = true;
foreach (string str in hiddenCols)
{
if (GridView1.Columns[k].HeaderText == str)
{
isHidden = false;
break;
}
}
if (isHidden)
columnbind.Append(GridView1.Columns[k].HeaderText + ',');
}
columnbind.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
bool isHidden = true;
foreach (string str in hiddenCols)
{
if (GridView1.Columns[k].HeaderText == str)
{
isHidden = false;
break;
}
}
if (isHidden)
{
columnbind.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
}
columnbind.Append("\r\n");
}
Response.Output.Write(columnbind.ToString());
Response.Flush();
Response.End();
}
Here is my current grid view.
<asp:GridView ID="grdIndexGroupMap" runat="server" AutoGenerateColumns="False" DataKeyNames="IndexName"
OnRowCancelingEdit="grdIndexGroupMap_RowCancelingEdit" OnRowDataBound="grdIndexGroupMap_RowDataBound"
OnRowEditing="grdIndexGroupMap_RowEditing" OnRowUpdating="grdIndexGroupMap_RowUpdating"
OnRowCommand="grdIndexGroupMap_RowCommand" ShowFooter="True" OnRowDeleting="grdIndexGroupMap_RowDeleting"
CellPadding="1" CellSpacing="1" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<%--IndexName--%>
<asp:TemplateField HeaderText="IndexName" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:DropDownList ID="cmbIndexName" runat="server" DataTextField="LocationName" DataValueField="IndexId"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblIndexName" runat="server" Text='<%# Eval("IndexName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cmbNewIndexName" runat="server" DataTextField="IndexName" DataValueField="IndexId"></asp:DropDownList>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
How do replace the DropDownList with a dropdown where I can select multiple items?
A checkboxlist in dropdownlist or a listbox with multiselect in dropdown. When selected will show comma seperated values.
Tried a couple of ways but wont work.
here is my databound method:
protected void grdIndexGroupMap_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList cmbIndexName = (DropDownList)e.Row.FindControl("cmbIndexName");
if (cmbIndexName != null)
{
cmbIndexName.DataSource = _Indexes;
cmbIndexName.DataTextField = "IndexName";
cmbIndexName.DataValueField = "IndexId";
cmbIndexName.DataBind();
cmbIndexName.SelectedValue = grdIndexGroupMap.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList cmbNewIndexName = (DropDownList)e.Row.FindControl("cmbNewIndexName");
cmbNewIndexName.DataSource = _Indexes;
cmbNewIndexName.DataBind();
}
}
I am using ASP.Net, C#
How about:
<asp:TemplateField HeaderText="IndexName" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:PlaceHolder id="phListContainer" runat="server" />
</EditItemTemplate>
then in your code-behind:
if (e.Row.RowType == DataControlRowType.DataRow)
{
phListContainer = (PlaceHolder)e.Row.FindControl(phListContainer);
if (phListContainer != null)
{
//Adding a DropDownList
var cmbIndexName = new DropDownList();
cmbIndexName.DataSource = _Indexes;
cmbIndexName.DataTextField = "IndexName";
cmbIndexName.DataValueField = "IndexId";
cmbIndexName.DataBind();
cmbIndexName.SelectedValue = grdIndexGroupMap.DataKeys[e.Row.RowIndex].Values[1].ToString();
phListContainer.Controls.Add(cmbIndexName);
// OR
//Adding a CheckBoxList;
cmbIndexName = new CheckBoxList();
cmbIndexName.DataSource = _Indexes;
cmbIndexName.DataTextField = "IndexName";
cmbIndexName.DataValueField = "IndexId";
cmbIndexName.DataBind();
phListContainer.Controls.Add(cmbIndexName);
}
}
Then you can get the controls by
var cbList = (CheckBoxList)e.Row.FindControl(phListContainer).Controls[0];
and then you can loop through and find the checked boxes
for(int i = 0; i < cbList.Items.Count; ++i)
{
if(cbList.Items[i].Selected)
//Do stuff
}
so let say i want to create paging like "> 1 2 3 4 5 6 <" and i decided to use paging template in gridview and this is what i do :
aspx file :
<asp:GridView ID="gvDept" runat="server" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
AllowSorting="True" AllowPaging="True" AllowCustomPaging="True"
DataSourceID="DS" DataKeyNames="departementcode" PageSize="3" OnRowCommand="gvDept_RowCommand">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="cbSelectAll" onclick="SelectAll(this)"></asp:CheckBox>
<%-- <asp:CheckBox runat="server" ID="cbAll" OnCheckedChanged="cbAll_CheckedChanged" AutoPostBack="true"></asp:CheckBox>--%>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbSelectOne"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField text="Edit" DataNavigateUrlFields="departementcode"
DataNavigateUrlFormatString="ManageDepartement.aspx?flag=edit&departementcode={0}" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm ('Are you sure?');" Text="Delete">
</asp:LinkButton>
</ItemTemplate>
<ItemStyle ForeColor="Red" />
</asp:TemplateField>
<asp:BoundField DataField="departementcode" HeaderText="Departement Code" ReadOnly="True" SortExpression="departementcode" />
<asp:BoundField DataField="departementname" HeaderText="Departement Name" SortExpression="departementname" />
<asp:BoundField DataField="createby" HeaderText="Create By" SortExpression="createby" />
<asp:BoundField DataField="createdate" HeaderText="Create Date" SortExpression="createdate" />
<asp:BoundField DataField="updateby" HeaderText="Update By" SortExpression="updateby" />
<asp:BoundField DataField="lastupdate" HeaderText="Last Update" SortExpression="lastupdate" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black"/>
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerSettings Mode="Numeric" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" CssClass="paging"/>
<PagerTemplate>
</PagerTemplate>
<RowStyle BorderColor="White"/>
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
<asp:LinqDataSource ID="DS" runat="server" ContextTypeName="Admin.AirAsiaDCDataContext"
EntityTypeName="" TableName="departements" EnableDelete="True" OnDeleted="DS_Deleted">
</asp:LinqDataSource>
and then i also put this code in C# :
protected void Page_Load(object sender, EventArgs e)
{
AirAsiaDCDataContext dc = new AirAsiaDCDataContext();
int dtcount = dc.departements.Count();
int pgcount = 0;
if (dtcount % 3 > 0)
{
pgcount = (dtcount / 3) + 1;
}
else
{
pgcount = pgcount / 3;
}
Button pagerBTN;
for (int i = 1; i <= pgcount; i++)
{
if(i == 1)
{
pagerBTN = new Button();
pagerBTN.ID = "btNext";
pagerBTN.Text = ">";
pagerBTN.CommandName = "Next";
gvDept.BottomPagerRow.Cells[0].Controls.Add(pagerBTN);
}
pagerBTN = new Button();
pagerBTN.ID = "bt" + i;
pagerBTN.Text = i.ToString();
pagerBTN.CommandName = i.ToString();
gvDept.BottomPagerRow.Cells[0].Controls.Add(pagerBTN);
if (i == pgcount)
{
pagerBTN = new Button();
pagerBTN.ID = "btLast";
pagerBTN.Text = "<";
pagerBTN.CommandName = "Last";
gvDept.BottomPagerRow.Cells[0].Controls.Add(pagerBTN);
}
}
}
protected void gvDept_RowCommand(object sender, GridViewCommandEventArgs e)
{
AirAsiaDCDataContext dc = new AirAsiaDCDataContext();
int datacount = dc.departements.Count();
int pagecount = 0;
int prevIDX = 0;
if (datacount % 3 > 0)
{
pagecount = (datacount / 3) + 1;
}
else
{
pagecount = datacount / 3;
}
if (e.CommandName == "Next")
{
if (gvDept.PageIndex < pagecount - 1)
{
prevIDX = gvDept.PageIndex;
gvDept.PageIndex = gvDept.PageIndex + 1;
if (prevIDX == pagecount - 2)
{
Button btn = (Button)gvDept.BottomPagerRow.Cells[0].FindControl("btNext");
btn.Visible = false;
}
}
}
else if (e.CommandName == "Prev")
{
if (gvDept.PageIndex > 0)
{
prevIDX = gvDept.PageIndex;
gvDept.PageIndex = gvDept.PageIndex - 1;
if (prevIDX == 1)
{
Button btn = (Button)gvDept.BottomPagerRow.Cells[0].FindControl("btPrev");
btn.Visible = false;
}
}
}
else
{
gvDept.PageIndex = Convert.ToInt32(e.CommandName) - 1;
}
}
my code working, but the problem is, if i click one of those paging button, after postback all my paging button which i generated before totally gone, i don't get it what i do wrong here? any correction?
In the grid view use
OnPageIndexChanging="gvDept_PageIndexChanging"
Add this code in .cs file to bind the data to grid view on each page index change.
protected void gvDept_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDept.PageIndex = e.NewPageIndex;
// write here function to bind the grid to the datasource
}
Add this at the starting in Page_Load function so that the data gets bind to the grid view each time after postback.
if (!IsPostBack)
{
//function to bind the grid to data source
}
I already spend 2 days trying to solve this issue but no luck on how to solve this. I have a GridView to display data from the database then it also have functionality to modify and delete. This is the current ASP code for my GridView:
<asp:GridView ID="dgvSortKey" runat="server" AllowSorting="True" OnRowDataBound="gv_drb"
AutoGenerateColumns="False" AllowPaging="True" BackColor="White" BorderColor="#336666"
BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
Height="73px" AutoGenerateEditButton="True" OnRowEditing="dgvSortKey_RowEditing"
OnRowUpdating="dgvSortKey_RowUpdating" OnRowCancelingEdit="dgvSortKey_RowCancelingEdit"
OnSelectedIndexChanging="dgvSortKey_SelectedIndexChanging" OnPageIndexChanged="dgvSortKey_PageIndexChanged"
OnPageIndexChanging="dgvSortKey_PageIndexChanging" OnRowCommand="dgvSortKey_RowCommand"
OnRowDeleted="dgvSortKey_RowDeleted" OnRowUpdated="dgvSortKey_RowUpdated" Width="561px"
PageSize="15" DataKeyNames="KeyCode,KeyDescription">
<FooterStyle BackColor="White" ForeColor="#333333" />
<RowStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="LightCyan" />
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Instruction Key Code">
<ItemTemplate>
<asp:Label ID="lblValKeyCode" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode((string)Eval("KeyCode")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#Bind("KeyCode") %>' MaxLength="10"
CausesValidation="false"></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="GvBorderGreen" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblValKeyDescription" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode((string)Eval("KeyDescription")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtValKeyDescription" runat="server" Text='<%#Bind("KeyDescription") %>'
Width="300" MaxLength="20" CausesValidation="false"></asp:TextBox>
</EditItemTemplate>
<ItemStyle CssClass="GvBorderGreen" />
</asp:TemplateField>
</Columns>
</asp:GridView>
The problem is I can't update a certain record after click the Update Button, please see the image below:
When I'm in debug mode, it does not pass on OnRowUpdating event instead it passes to OnRowEditing. One thing that it makes me surprise is that when it fires to OnRowCommand, the CommandName set to "Edit" when Update Button is clicked. Please see the image below:
BTW this the Code Behind.
protected void dgvSortKey_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Currently Unreachable code due to unknown reason. RowUpdating not trigger even clicking Update Button in the GridView
// this.dvDetialContent.Visible = true;
List<SqlDbParameter> list = new List<SqlDbParameter>();
TextBox txtValKeyDescription = dgvSortKey.Rows[e.RowIndex].FindControl("txtValKeyDescription") as TextBox;
TextBox txtValKeyCode = dgvSortKey.Rows[e.RowIndex].FindControl("txtValKeyCode") as TextBox;
if (string.IsNullOrEmpty(txtValKeyCode.Text) || string.IsNullOrEmpty(txtValKeyDescription.Text))
{
string alert = "alert('Instruction KeyCode or KeyDecription should not be empty');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "scripting", alert, true);
}
else
{
//Trace.Write("txtKeyCode", HttpUtility.HtmlDecode(txtKeyCode.Text));
//Trace.Write("txtKeyDescription", txtKeyDescription.Text);
//Trace.Write("txtValKeyCode", txtValKeyCode.Text);
//Trace.Write("txtValKeyDescription", txtValKeyDescription.Text);
list.Add(new SqlDbParameter("#oldcode", HttpUtility.HtmlDecode(txtKeyCode.Text)));
list.Add(new SqlDbParameter("#oldname", HttpUtility.HtmlDecode(txtKeyDescription.Text)));
list.Add(new SqlDbParameter("#newcode", HttpUtility.HtmlDecode(txtValKeyCode.Text)));
list.Add(new SqlDbParameter("#newname", HttpUtility.HtmlDecode(txtValKeyDescription.Text)));
try
{
int result = CoreUtility.ExecuteNonQuery("update InstructionKey set KeyCode=#newcode, KeyDescription=#newname where KeyCode = #oldcode and KeyDescription = #oldname", list);
//Trace.Write("ResultValue", result.ToString());
}
catch (Exception ex)
{
string alert = "alert('Instruction KeyCode should not be duplicate');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "scripting2", alert, true);
}
}
dgvSortKey.EditIndex = -1;
imgbtnFilter_Click(null, null);
this.txtKeyCode.Text = "";
this.txtKeyDescription.Text = "";
}
protected void dgvSortKey_RowEditing(object sender, GridViewEditEventArgs e)
{
// this.dvDetialContent.Visible = true;
//if (ViewState["updateFlag"] == null)
//{
//ViewState["editFlag"] = "forEdit";
//ViewState["editIndex"] = e.NewEditIndex;
dgvSortKey.EditIndex = e.NewEditIndex;
Label lblValKeyDescription = dgvSortKey.Rows[e.NewEditIndex].FindControl("lblValKeyDescription") as Label;
Label lblValKeyCode = dgvSortKey.Rows[e.NewEditIndex].FindControl("lblValKeyCode") as Label;
this.txtKeyCode.Text = lblValKeyCode.Text;
this.txtKeyDescription.Text = lblValKeyDescription.Text;
imgbtnFilter_Click(null, null);
//}
//else
//{
//RowUpdate((int)ViewState["editIndex"]);
//ViewState.Remove("updateFlag");
//ViewState.Remove("editFlag");
//ViewState.Remove("editIndex");
//}
}
protected void dgvSortKey_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
dgvSortKey.EditIndex = -1;
imgbtnFilter_Click(null, null);
}
protected void dgvSortKey_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
this.dvDetialContent.Visible = true;
Label lblValKeyDescription = dgvSortKey.Rows[e.NewSelectedIndex].FindControl("lblValKeyDescription") as Label;
Label lblValKeyCode = dgvSortKey.Rows[e.NewSelectedIndex].FindControl("lblValKeyCode") as Label;
this.txtKeyCode.Text = lblValKeyCode.Text;
this.txtKeyDescription.Text = lblValKeyDescription.Text;
}
protected void dgvSortKey_PageIndexChanged(object sender, EventArgs e)
{
}
protected void dgvSortKey_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
dgvSortKey.PageIndex = e.NewPageIndex;
imgbtnFilter_Click(null, null);
}
protected void dgvSortKey_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
protected void dgvSortKey_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String jsScript = "";
jsScript += "var answer=confirm('Delete this Instruction Key?');\n";
jsScript += "if (!answer){\n";
jsScript += " document.getElementById('ctl00_cthContent_hdDelete').Value = 'DELETE';\n";
jsScript += "}\n";
//return;
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", jsScript, true);
List<SqlDbParameter> list = new List<SqlDbParameter>();
Label lblValKeyCode = dgvSortKey.Rows[e.RowIndex].FindControl("lblValKeyCode") as Label;
Label lblValKeyDescription = dgvSortKey.Rows[e.RowIndex].FindControl("lblValKeyDescription") as Label;
list.Add(new SqlDbParameter("#code", lblValKeyCode.Text));
list.Add(new SqlDbParameter("#name", lblValKeyDescription.Text));
CoreUtility.ExecuteNonQuery("DELETE FROM [InstructionKey] WHERE KeyCode=#code and KeyDescription=#name;", list);
Initial();
this.dvDetialContent.Visible = false;
dgvSortKey.EditIndex = -1;
imgbtnFilter_Click(null, null);
}
protected void dgvSortKey_RowCommand(object sender, GridViewCommandEventArgs e)
{
string id = e.CommandName;
}
protected void dgvSortKey_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
}
protected void gv_drb(object sender, GridViewRowEventArgs e)//
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
//raising javascript confirmationbox whenver user clicks on link button
lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox()");
}
}
While I have not figured out the bug, there is a workaround you may want to try.
Since the autogenerated edit button is giving you trouble, why not generate it yourself?
Like this:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lnkedit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkedit" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="lnkedit" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
And handle it in your code behind:
void dgvSortKey_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Update")
{
}
}