I want to edit the record in gridview on single click. My gridview is placed inside update panel.
There is a search textbox given and when I type anything in search textbox "txt_Search_TextChanged" event is fired and that text is searched in database and that particular record is fetched and bind to the grid.
Now my problem is: If I click on edit button of grid without searching, then event is fired. But if I search any record and then I click on edit button, it takes 2 click to give result. I have written a script for textbox which is as follows:
enter code here
<script>
function RefreshUpdatePanel() {
__doPostBack('<%= txt_Search.ClientID %>', '');
};
below is aspx code
<table width="100%">
<tr>
<td align="center">
<div style="border-width: 2px; border-style: solid; border-color: #FDAC4C; width: 95%; border-radius: 10px;">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:GridView ID="grd_Master" ShowFooter="false" runat="server" Width="98%" AllowPaging="true" PagerStyle-CssClass="paging"
OnPageIndexChanging="grd_Master_PageIndexChanging"
PageSize="8" AutoGenerateColumns="False" BorderWidth="0" BorderStyle="Solid" BorderColor="#FDAC4C" OnRowEditing="grd_Master_RowEditing" ShowHeaderWhenEmpty="true">
<AlternatingRowStyle BackColor="#FFFFFF" Height="30px" BorderStyle="Dotted" BorderWidth="1"
BorderColor="#DDDDDD" />
<RowStyle BackColor="#FFF0DD" Height="30px" BorderStyle="Dotted" BorderWidth="1"
BorderColor="#DDDDDD" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Serial No.<br />
</HeaderTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle HorizontalAlign="Right" Width="10%" CssClass="grdrow"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblSRNO" runat="server" Style="padding-right: 50px" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
<asp:Label ID="Cooling_Code" runat="server" Visible="false" Text='<% #Bind("Cool_Code")%>'></asp:Label>
<asp:Label ID="lblProdGrpCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Prod_Grp_Code")%>' Visible="false"></asp:Label>
<asp:Label ID="lblSubProdGrpCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Sub_Prod_Code")%>' Visible="false"></asp:Label>
<asp:Label ID="lblMakeCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Make_Code")%>' Visible="false"></asp:Label>
<asp:Label ID="lblRegerCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Refg_Code")%>' Visible="false"></asp:Label>
<asp:Label ID="lblCompressorCode" runat="server" Style="padding-left: 100px" Text='<% #Bind("Comp_Code")%>' Visible="false"></asp:Label>
<asp:Label ID="lblFilePath" runat="server" Visible="false" Style="padding-left: 100px" Text='<% #Bind("Document_Path")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="grdheader" Width="10%" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Cooling Name
</HeaderTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle HorizontalAlign="Left" Width="20%" CssClass="grdrow"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblCoolingName" runat="server" Style="padding-left: 10px" Text='<% #Bind("Cool_Name")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="grdheader" Width="20%" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Short Name
</HeaderTemplate>
<HeaderStyle HorizontalAlign="Left" CssClass="grdheader" Width="15%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left" Width="15%" CssClass="grdrow"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblCoolingShortName" runat="server" Style="padding-left: 10px" Text='<% #Bind("Cool_Short_Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Cooling Description
</HeaderTemplate>
<ItemStyle HorizontalAlign="left" Width="45%" CssClass="grdrow"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblCoolingDescr" runat="server" Style="padding-left: 10px" Text='<% #Bind("Cool_Descr")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" CssClass="grdheader" Width="45%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Action<br />
</HeaderTemplate>
<HeaderStyle HorizontalAlign="Left" CssClass="grdheader" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="10%" CssClass="grdrow"></ItemStyle>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/images/Edit.png" Width="20px" ToolTip="Edit" CommandName="Edit" Height="20px" />
<asp:ImageButton ID="ImageDelete" runat="server" ImageUrl="~/images/trash1.png" ToolTip="Delete" OnClick="ImageDelete_Click" Width="25px" Height="20px" OnClientClick="return confirmDelete()" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Left" BorderWidth="0" BorderStyle="None" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txt_Search" />
<asp:PostBackTrigger ControlID="grd_Master" />
</Triggers>
</asp:UpdatePanel>
</div>
</td>
</tr>
</table>
Below is cs code for rowediting
protected void grd_Master_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
tab0.Enabled = false;
tab1.Enabled = true;
grd_Master.EditIndex = e.NewEditIndex;
GridViewRow rw = (GridViewRow)grd_Master.Rows[e.NewEditIndex];
Label CoolingCode = (Label)rw.FindControl("Cooling_Code");
Label ProdgrpCode = (Label)rw.FindControl("lblProdGrpCode");
Label SubProdCode = (Label)rw.FindControl("lblSubProdGrpCode");
Label MakeCode = (Label)rw.FindControl("lblMakeCode");
Label ReferCode = (Label)rw.FindControl("lblRegerCode");
Label CompressoreCode = (Label)rw.FindControl("lblCompressorCode");
Label CoolingName = (Label)rw.FindControl("lblCoolingName");
Label CoolingName_Short = (Label)rw.FindControl("lblCoolingShortName");
Label CoolingName_Desc = (Label)rw.FindControl("lblCoolingDescr");
Label lblFilePath = (Label)rw.FindControl("lblFilePath");
txtCoolType.Text = CoolingName.Text.Trim();
txtCoolTypeShortname.Text = CoolingName_Short.Text.Trim();
txtCoolTypeDescrip.Text = CoolingName_Desc.Text.Trim();
ddlProdGrp.SelectedValue = ProdgrpCode.Text;
ddlSubProd.SelectedValue = SubProdCode.Text;
ddlMake.SelectedValue = MakeCode.Text;
ddlCompressor.SelectedValue = CompressoreCode.Text;
ddlRefe.SelectedValue = ReferCode.Text;
btnsave.Text = "Update";
ViewState["CoolingCode"] = CoolingCode.Text;
TabContainer_Item.ActiveTabIndex = 1;
Session["CoolingCode"] = CoolingCode.Text;
this.fu1.BindGridDoc();
}
catch (Exception ex)
{
Master.ErrorMessage(ex.Message);
}
}
protected void txt_Search_TextChanged(object sender, EventArgs e)
{
try
{
DataSet ds = gn.ExecuteDataset("Search_CoolingType", txt_Search.Text.Trim());
ViewState["CoolingTypeData"] = ds.Tables[0];
if (ds.Tables[0].Rows.Count == 0)
{
FillEmptyGrid();
}
else
{
grd_Master.DataSource = ds;
grd_Master.DataBind();
}
}
catch (Exception ex)
{
Master.ErrorMessage(ex.Message);
}
}
You need to make sure that the grid has focus. The initial click is likely just setting focus on the grid, and then it sees the second click as starting your edit.
A simple ActiveControl = grd_Master; at the end of the txt_Search_TextChanged event should do the trick.
Related
Am face some troubles to pass data from inner grid to main grid as shown in this pic
im tried too many solution i can found using C# and Jquery
this is simple photo to result
Now this is invoice read from database to show multiple data as shown the grid contain a another grid by normal code it was impossible to access to gridview2 to get data
all i want is get "amount" from gridview2 then calculate "sum" then pass it to "total" in gridview1
also im trid with Jquery it do nothing this is simple code of both fields i want to do sum with Jquery
<script type="text/javascript">
$(function () {
var fields = document.getElementsByClassName('clstocal');
var sum = 0;
for (var i = 0; i < fields.length; ++i) {
var item = fields[i];
sum += parseInt(item.innerHTML);
}
$("#totalprice").text(sum);
}); // this code get it from this site
</script>
<asp:TextBox ID="amount" CssClass ="clstocal" autopostback="true" runat="server" Enabled="False" Height="24px" Text='<%# Bind("body_total") %>' Width="98px" OnTextChanged="amount_TextChanged"></asp:TextBox>
and
<asp:TextBox ID="totalprice" runat="server" Enabled="False" Height="22px" Width="145px" OnTextChanged="totalprice_TextChanged" autopostback="true"></asp:TextBox>
there are a easy solution but not a good solution is sum the result to database then read it again but this is not efficient at all ..
To be honest the answer was simple
1. its impossible to access grid inside grid
2. solved by normal code in C# as shown
protected void GridHead(object sender, GridViewRowEventArgs e)
{
// MAin Grid which is grid number 1
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox textboxPrice1 = e.Row.FindControl("totalprice") as TextBox;
textboxPrice1.Text = i.ToString();
i = 0; // Global double -> protected double i = 0; // THIS LINE TO STOP COUNTING TO OTHER NEXT INVOICE
}
}
protected void GridBody(object sender, GridViewRowEventArgs e)
{
// Inner grid which contain multipe item need to sun then send it to Main grid
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox lblPrice2 = e.Row.FindControl("amount") as TextBox;
i += Convert.ToDouble(lblPrice2.Text);
}
}
and the code behind to handle the grid
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Reports._Default" %>
Welcome to Invoice Revision
<!-- change both grid from OnSelectedIndexChanged to onrowdatabound to use GridViewRowEventArgs instade EventArgs-->
<asp:GridView ID="GD_Head" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="206px" Width="548px" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" onrowdatabound="GridHead">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<br />
<asp:Label ID="Label25" runat="server" Height="22px" Text="Invoice : " Width="60px"></asp:Label>
<asp:Label ID="Label1" width="145px" runat="server" Text='<%# Eval("invoice") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px" Font-Size="Smaller"></asp:Label>
<asp:Label ID="Label26" runat="server" Height="22px" Text="Kind : " Width="60px"></asp:Label>
<asp:Label ID="Label35" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("kind") %>' Width="71px"></asp:Label>
<asp:Label ID="Label27" runat="server" Height="22px" Text="Date : " Width="60px"></asp:Label>
<asp:Label ID="Label3" width="170px" runat="server" Text='<%# Eval("date") %>' BorderWidth="1px" Height="22px"></asp:Label>
<br />
<br />
<asp:Label ID="Label28" runat="server" Height="22px" Text="File : " Width="60px"></asp:Label>
<asp:Label ID="Label4" width="144px" runat="server" Text='<%# Eval("file") %>' BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label29" runat="server" Height="22px" Text="Awb" Width="60px"></asp:Label>
<asp:Label ID="Label5" width="313px" runat="server" Text='<%# Eval("awb") %>' BorderWidth="1px" Height="22px"></asp:Label>
<br />
<br />
<asp:Label ID="Label30" runat="server" Height="22px" Text="GL : " Width="60px"></asp:Label>
<asp:Label ID="Label6" width="144px" runat="server" Text='<%# Eval("gl") %>' BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label7" width="376px" runat="server" Text='<%# Eval("name") %>' BorderWidth="1px" Height="23px"></asp:Label>
<br />
<br />
<asp:Label ID="Label31" runat="server" Height="22px" Text="Total : " Width="60px"></asp:Label>
<!-- Convert String to int then sum them from body to head -->
<asp:TextBox ID="totalprice" OnTextChanged ="totalprice_TextChanged" runat="server" Enabled="False" Height="22px" Width="145px"></asp:TextBox>
<asp:Label ID="lb12" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("curId") %>' Width="38px"></asp:Label>
<asp:Label ID="Label32" runat="server" Height="22px" Text="Rate : " Width="40px"></asp:Label>
<asp:Label ID="Label9" width="100px" runat="server" Text='<%# Eval("rate") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label33" runat="server" Height="22px" Text="Emp" Width="40px"></asp:Label>
<asp:Label ID="lb10" width="65px" runat="server" Text='<%# Eval("emp") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label34" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("poste") %>' Width="62px"></asp:Label>
<br />
<br />
<asp:Label ID="Label14" runat="server" Height="22px" Text="No" Width="52px"></asp:Label>
<asp:Label ID="Label23" width="76px" runat="server" Text='GL' Height="22px"></asp:Label>
<asp:Label ID="Label18" runat="server" Height="22px" Text="Description" Width="250px"></asp:Label>
<asp:Label ID="Label19" runat="server" Height="22px" Text="Amount" Width="130px"></asp:Label>
<!-- change both grid from OnSelectedIndexChanged to onrowdatabound to use GridViewRowEventArgs instade EventArgs-->
<asp:GridView ID="GD_Body" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="serial" DataSourceID="SqlDataSource2" Height="39px" onrowdatabound="GridBody" Width="629px" GridLines="None">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Label ID="lb40" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("line_no") %>' Width="52px"></asp:Label>
<asp:Label ID="Label41" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("body_gl") %>' Width="76px"></asp:Label>
<asp:Label ID="Label42" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("body_name") %>' Width="250px"></asp:Label>
<asp:TextBox ID="amount" CssClass ="clstocal" runat="server" Enabled="False" Height="24px" Text='<%# Bind("body_total") %>' Width="98px"></asp:TextBox>
<asp:Label ID="UN" runat="server" BorderWidth="1px" Height="22px" Text="UN" Width="52px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Label ID="Label22" runat="server" Text="---------------------------------------------------------------------------------------------------------------------" Width="600px"></asp:Label>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Body %>" SelectCommand="SELECT * FROM [body] WHERE ([invoice] = #invoice)">
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="invoice" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
</ItemTemplate>
<ControlStyle Font-Bold="True" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Head %>" SelectCommand="SELECT * FROM [head]"></asp:SqlDataSource>
and this is result
I hope this answer find you
if you have better answer please share with us
I am trying to implement GO TO page.Means when i enter any page number in text box "txtGoToPage" and clicking button "btnGo" it should take me to that page.
<asp:Label ID="lblGoToPage" runat="server" Text="Go To Page : "></asp:Label>
<asp:TextBox ID="txtGoToPage" runat="server" Width="47px"></asp:TextBox>
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_RowDataBound" OnRowDeleting="gv_RowDeleting" OnRowEditing="gv_RowEditing" CellPadding="4" ForeColor="#333333" OnRowCreated="gv_RowCreated" OnRowCommand="gv_RowCommand" OnRowUpdating="gv_RowUpdating" AllowPaging="true" OnPageIndexChanging="gv_PageIndexChanging" GridLines="Both" CssClass="GridViewStyle" class="ui-widget-content" Style="width: 100%; padding: 2px" OnSelectedIndexChanged="gv_SelectedIndexChanged">
<EditRowStyle CssClass="GridViewEditRow" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle CssClass="pager" />
<Columns>
<asp:TemplateField Visible="false" HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Correct">
<ItemTemplate>
<asp:Label ID="lblCorrect" runat="server" Text='<%#Eval("correct") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCorrect" runat="server" Width="40px" Text='<%#Eval("correct") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<asp:Label ID="lblComments" runat="server" Text='<%#Eval("comments") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtComments" runat="server" TextMode="multiline" Width="50px" Columns="50" Rows="5" Text='<%#Eval("comments") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last"
Mode="Numeric" PageButtonCount="20" PreviousPageText="Previous" />
</asp:GridView>
Thanks in advance.
Sample answer:
protected void btnGo_Click(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt16(txtGoToPage.Text) -1; //since PageIndex starts from 0 by default.
txtGoToPage.Text = "";
GridView1.DataBind()
}
I have a nested GridView to 5 levels and I am having hard time accessing which RadioButtonList is selected in 2nd level of GridView. (first level is named gvSecondLevel so please don't get confused). I have to now access from gvThirdLevel (which is actually 2nd level grid as you will see in code). I am not sure whether the issue is with ASPX code or c# code. I have tried putting rbThirdLevel inside TemplateField and without it. But I can't access it either way. Here is the aspx code:
<table align="left">
<tr>
<td colspan="2">
<asp:GridView ID="gvSecondLevel" runat="server" AutoGenerateColumns="False" Width="800px"
ShowFooter="True" BorderStyle="none" DataKeyNames="CallCategory_ID" OnRowDataBound="gvSecondLevel_OnRowDataBound"
OnRowCommand="gvSecondLevel_RowCommand" OnRowDeleting="gv_RowDeleting" AllowPaging="true"
PageSize="10" OnPageIndexChanging="gvSecondLevel_PageIndexChanging" OnRowCreated="gvSecondLevel_RowCreated" >
<Columns>
<asp:TemplateField HeaderText="ID" HeaderStyle-Width="20px">
<ItemTemplate>
<asp:Label ID="lblBusinessID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"CallCategory_ID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="20px" />
</asp:TemplateField>
<asp:BoundField DataField="CallCategory_Name" HeaderText="Call Category Name 2nd" />
<asp:TemplateField HeaderText="" ItemStyle-Width="20px">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteSecondLevel" CommandArgument='<%# Eval("CallCategory_ID") %>'
CommandName="Delete" runat="server"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="20px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BorderStyle="None" FooterStyle-BorderStyle="None"
ItemStyle-BorderStyle="None">
<ItemTemplate>
<tr>
<td id="td_div<%# Eval("CallCategory_ID") %>" colspan="100%" style="border: none;">
<div id="div<%# Eval("CallCategory_ID") %>" style="overflow: auto; display: none;
position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvThirdLevel" runat="server" Width="95%" AutoGenerateColumns="false"
ShowFooter="true" BorderStyle="none" DataKeyNames="CallCategory_ID" OnRowDataBound="gvThirdLevel_OnRowDataBound"
OnRowCommand="gvThirdLevel_RowCommand" OnRowDeleting="gv_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="true" HeaderStyle-Width="20px">
<ItemTemplate>
<asp:Label ID="lblCallCategoryID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CallCategory_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CallCategory_Name" HeaderText="Call Category Name 3rd" />
<asp:TemplateField HeaderText="" ItemStyle-Width="20px">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteThirdLevel" CommandArgument='<%# Eval("CallCategory_ID") %>'
CommandName="Delete" runat="server">Deactivate</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BorderStyle="None" FooterStyle-BorderStyle="None"
ItemStyle-BorderStyle="None">
<ItemTemplate>
<tr>
<td id="td_div1<%# Eval("CallCategory_ID") %>" colspan="100%" style="border: none;">
<div id="div1<%# Eval("CallCategory_ID") %>" style="overflow: auto; display: none;
position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvFourthLevel" runat="server" Width="95%" AutoGenerateColumns="false"
BorderStyle="none" ShowFooter="true" OnRowDataBound="gvFourthLevel_OnRowDataBound"
OnRowCommand="gvFourthLevel_RowCommand" OnRowDeleting="gv_RowDeleting" DataKeyNames="CallCategory_ID">
<Columns>
<asp:TemplateField HeaderText="ID" HeaderStyle-Width="20px">
<ItemTemplate>
<asp:Label ID="lblCallCategoryID_Fourth" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CallCategory_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CallCategory_Name" HeaderText="Call Category Name 4th" />
<asp:TemplateField HeaderText="" ItemStyle-Width="20px">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteFourthLevel" CommandArgument='<%# Eval("CallCategory_ID") %>'
CommandName="Delete" runat="server">Deactivate</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BorderStyle="None" FooterStyle-BorderStyle="None"
ItemStyle-BorderStyle="None">
<ItemTemplate>
<tr>
<td id="td_div2<%# Eval("CallCategory_ID") %>" colspan="100%" style="border: none;">
<div id="div2<%# Eval("CallCategory_ID") %>" style="overflow: auto; display: none;
position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvFifthLevel" runat="server" Width="95%" AutoGenerateColumns="false"
BorderStyle="none" ShowFooter="true" OnRowDataBound="gvFifthLevel_OnRowDataBound"
OnRowCommand="gvFifthLevel_RowCommand" OnRowDeleting="gv_RowDeleting" DataKeyNames="CallCategory_ID">
<Columns>
<asp:TemplateField HeaderText="ID" HeaderStyle-Width="20px">
<ItemTemplate>
<asp:Label ID="lblCallCategoryID_Fifth" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CallCategory_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CallCategory_Name" HeaderText="Call Category Name 5th" />
<asp:TemplateField HeaderText="" ItemStyle-Width="20px">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteFifthLevel" CommandArgument='<%# Eval("CallCategory_ID") %>'
CommandName="Delete" runat="server">Deactivate</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BorderStyle="None" FooterStyle-BorderStyle="None"
ItemStyle-BorderStyle="None">
<ItemTemplate>
<tr>
<td id="td_div3<%# Eval("CallCategory_ID") %>" colspan="100%" style="border: none;">
<div id="div3<%# Eval("CallCategory_ID") %>" style="overflow: auto; display: none;
position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvSixthLevel" runat="server" Width="95%" AutoGenerateColumns="false"
BorderStyle="none" ShowFooter="true" OnRowDataBound="gvSixthLevel_OnRowDataBound"
OnRowCommand="gvSixthLevel_RowCommand" OnRowDeleting="gv_RowDeleting">
<Columns>
<asp:BoundField DataField="CallCategory_ID" HeaderText="ID" HeaderStyle-Width="20px" />
<asp:BoundField DataField="CallCategory_Name" HeaderText="Call Category Name 6th" />
<asp:TemplateField HeaderText="" ItemStyle-Width="20px">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteSixthLevel" CommandArgument='<%# Eval("CallCategory_ID") %>'
CommandName="Delete" runat="server">Deactivate</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:RadioButtonList ID="rbSixthLevel" runat="server"
onselectedindexchanged="rbSixthLevel_SelectedIndexChanged" RepeatDirection="Horizontal" AutoPostBack="True">
<asp:ListItem Selected="True">Active</asp:ListItem>
<asp:ListItem>Inactive</asp:ListItem>
</asp:RadioButtonList>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:RadioButtonList ID="rbFifthLevel" runat="server" onselectedindexchanged="rbFifthLevel_SelectedIndexChanged" RepeatDirection="Horizontal" AutoPostBack="True">
<asp:ListItem Selected="True">Active</asp:ListItem>
<asp:ListItem>Inactive</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:RadioButtonList ID="rbFourthLevel" runat="server" onselectedindexchanged="rbFourthLevel_SelectedIndexChanged" RepeatDirection="Horizontal" AutoPostBack="True">
<asp:ListItem Selected="True">Active</asp:ListItem>
<asp:ListItem>Inactive</asp:ListItem>
</asp:RadioButtonList>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButtonList ID="rbThirdLevel" runat="server" onselectedindexchanged="rbThirdLevel_SelectedIndexChanged" RepeatDirection="Horizontal" AutoPostBack="True">
<asp:ListItem Selected="True">Active</asp:ListItem>
<asp:ListItem>Inactive</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:RadioButtonList ID="rbSecondLevel" runat="server" onselectedindexchanged="rbSecondLevel_SelectedIndexChanged"
RepeatDirection="Horizontal" AutoPostBack="True">
<asp:ListItem Selected="True">Active</asp:ListItem>
<asp:ListItem>Inactive</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>
C# code is just return "Active" even when I select "Inactive". Please guide what is the correct way to access rbThirdLevel
protected void rbThirdLevel_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvSecondLevel.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
GridView gvThirdLevel = (GridView)row.FindControl("gvThirdLevel");
if (gvThirdLevel != null)
{
RadioButtonList rbThirdLevel = gvSecondLevel.Rows[row.RowIndex].FindControl("rbThirdLevel") as RadioButtonList;
string radiovalue = (gvSecondLevel.Rows[row.RowIndex].FindControl("rbThirdLevel") as RadioButtonList).SelectedItem.Value;
if (rbThirdLevel.SelectedValue == "Inactive")
{
//code to display Deactivated rows
}
if (rbThirdLevel.SelectedValue == "Active")
{
//code to display Activated rows
}
}
}
}
}
I have a datagrid and it has a checkbox in rows. I want to change background color of row when checkbox is clicked. Example code:
<asp:DataGrid ID="Grid" runat="server" DataKeyField="KeyID" CssClass="grid"
AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333"
GridLines="None" OnPageIndexChanged="Grid_PageIndexChanged"
OnEditCommand="Grid_EditCommand" OnDeleteCommand="GetResult"
onitemdatabound="Grid_ItemDataBound" >
<Columns>
<HeaderTemplate>
<input id="chkAllItems" type="checkbox" onclick="javascript:HeaderClick(this);"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "KeyID")%>' ID="checkBoxID" AutoPostBack="false" OnClick="chechkedChanged(this);" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<label for="male">Category</label>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox Text="" ID="Category" runat="server" AutoPostBack="false" MaxLength="10" CssClass="tb5" > </asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" HeaderText="Edit">
</asp:EditCommandColumn>
<asp:ButtonColumn CommandName="Delete" HeaderText="TDK" Text="tdk"></asp:ButtonColumn>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" Mode="NumericPages" />
<AlternatingItemStyle BackColor="#FAAC58" />
<ItemStyle BackColor="#81DAF5" ForeColor="#333333" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
How can I change background color of row in javascript ?
My simple GridView control contains Checkbox, Dept No, Dept Name, Location columns.
When clicked on CheckBox, the corresponding row background color changes to Red background. Javascript code inside .aspx page plays major role to achieve your functionality.
.aspx code is below:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function ChangeBackgroundColor(checkBox1) {
var parent = document.getElementById("<%= GridView1.ClientID %>");
var items = parent.getElementsByTagName("input");
for (i = 0; i < items.length; i++) {
if ((items[i].type == "checkbox") && (items[i].checked)) {
alert(items[i].id + ' is checked');
items[i].parentElement.parentElement.style.backgroundColor = "Red";
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" onclick="javascript:ChangeBackgroundColor(this);" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dept No">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("DeptNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dept name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("DName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("LOC") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code behind code is given below.
SqlConnection objConn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["STERIAConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
var objCmd = new SqlCommand("SELECT * FROM DEPT", objConn);
objConn.Open();
GridView1.DataSource = objCmd.ExecuteReader();
GridView1.DataBind();
objConn.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var checkbox1 = e.Row.FindControl("CheckBox1") as CheckBox;
checkbox1.Attributes.Add("OnClick","return ChangeBackgroundColor('checkbox1');");
}
}
Hope this code helps you. Let me know if you need some more help.
I Have the below Asp.net Code:
the problem is that When Timer Tick Every 30 Second and GridView Filled the javascript code is not executed !! and Sound is not played.
I have also tried SoundPlayer Class but is not working on the Server Side can anyone provide me with a solution ?
<%# Page Language="C#" AutoEventWireup="true" CodeFile="OrdersPage.aspx.cs" Inherits="Orders_" Async="true" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Gazar | Orders Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link rel="Stylesheet" href="StyleSheet.css" />
<script type="text/javascript">
function ShowProgress() {
document.getElementById('UpdateProgress1').style.display = "inline";
}
</script>
</head>
<body>
<form id="form1" class="center_it" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" EnablePageMethods="true" runat="server"></asp:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<table class="MainTable">
<tr>
<td style="text-align: left;" colspan="2">
<img id="Img2" src="~/images/gazzarlogo.png" alt="GazarBanner" runat="server" class="BannerImage" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="30pt" ForeColor="White"
Text="الطلبـــــات"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="History" CssClass="Buttons" />
</td>
<td>
<asp:Button ID="Exit" Text="خروج" runat="server" OnClick="Exit_Click" CssClass="Buttons" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridViewOrders" runat="server" Style="margin-left: auto; margin-right: auto;"
OnRowDataBound="GridViewOrders_RowDataBound" PageSize="30" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="0px" CaptionAlign="Top"
CellPadding="4" ForeColor="Black" HorizontalAlign="Center" EnableModelValidation="True"
GridLines="Vertical" Width="1100px" Font-Names="Tahoma" Font-Size="10pt" Font-Bold="False"
OnRowCommand="GridViewOrders_RowCommand" AutoGenerateColumns="False">
<RowStyle BackColor="#F7F7DE" Wrap="True" />
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Open" ItemStyle-Width="60px">
<ItemTemplate>
<asp:Button ID="ApproveBtn" Text="Open" Width="60" Height="30" runat="server" OnClientClick="ShowProgress()" />
</ItemTemplate>
<ItemStyle Width="70px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" ItemStyle-Width="70px">
<ItemTemplate>
<asp:Label ID="OrderID" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="70px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="Name" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" ItemStyle-Width="300px">
<ItemTemplate>
<asp:Label ID="Address" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Address") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="300px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Number" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Number" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Number") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Sent Date" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="SentDate" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Sent Date") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Recieved Date" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="RecievedDate" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Recieved Date") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="# Success" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="SuccessNo" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "# Success") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="# Fake" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="FakeNo" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "# Fake") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Restaurant" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="Restaurant" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Restaurant") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="Branch" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Branch") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<div class="container">
<div class="LoadingDiv">
<span class="LoadingLabel">Check Orders ...</span>
<img class="LoadingImg" src="Images/ajax-loader.gif" alt="Loading ..." />
</div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:Timer ID="Timer1" runat="server" Interval="30000" OnTick="Timer1_Tick" />
</form>
</body>
</html>
and code behind is:
protected void Timer1_Tick(object sender, EventArgs e)
{
orderDB.LogState("Time Tick");
if (Session["UserID"] != null)
{
orderDB.LogState("Session Alive");
FillDataGrid(int.Parse(Session["UserID"].ToString()));
orderDB.LogState("End fill Grid View");
}
else
{
Response.Redirect("~/login.aspx");
}
}
private void FillDataGrid(int UserID)
{
Orders = orderDB.GetOrdersByUser(UserID);
orderDB.LogState("Select: " + DateTime.Now.ToString() + "\t" + Request.UserHostAddress + "\t" + Session.SessionID + "\t" + Session["UserID"].ToString());
if (Orders == null)
Response.Redirect("~/login.aspx");
DataTable Table = new DataTable();
Table.Columns.Add("ID", System.Type.GetType("System.String"));
Table.Columns.Add("Name", System.Type.GetType("System.String"));
Table.Columns.Add("Address", System.Type.GetType("System.String"));
Table.Columns.Add("Number", System.Type.GetType("System.String"));
Table.Columns.Add("Sent Date", System.Type.GetType("System.String"));
Table.Columns.Add("Recieved Date", System.Type.GetType("System.String"));
Table.Columns.Add("# Success", System.Type.GetType("System.String"));
Table.Columns.Add("# Fake", System.Type.GetType("System.String"));
Table.Columns.Add("Restaurant", System.Type.GetType("System.String"));
Table.Columns.Add("Branch", System.Type.GetType("System.String"));
for (int i = 0; i < Orders.Count; i++)
{
object[] myRow = new object[10];
myRow[0] = Orders[i].GetId();
myRow[1] = Orders[i].GetOwner();
myRow[2] = Orders[i].GetAddress().getAllAddress();
myRow[3] = Orders[i].GetPhoneNumber();
myRow[4] = Orders[i].GetOriginalTime().ToString();
myRow[5] = Orders[i].GetTime().ToString();
myRow[6] = Orders[i].NoOfSuccess; //success Orders;
myRow[7] = Orders[i].NoOfFake; //fake Orders
myRow[8] = Orders[i].GetResName();
myRow[9] = Orders[i].GetBranchName();
Table.Rows.Add(myRow);
}
orderDB.LogState("Table Rows Count: " + Table.Rows.Count);
GridViewOrders.DataSource = Table;
GridViewOrders.DataBind();
string sSelectedAudio = Server.MapPath("home.wav");
if (GridViewOrders.Rows.Count != 0)
{
playSound(sSelectedAudio);
orderDB.LogState("Sound Played: " + GridViewOrders.Rows.Count);
}
else
{
orderDB.LogState("Empty Grid View: " + GridViewOrders.Rows.Count);
}
}
private void playSound(string path)
{
string PlaySound = "<embed src=\"" + path + "\" autostart=\"true\" hidden=\"true\"></embed>";
Response.Write(PlaySound);
}
It could be the Response.Write that's causing the issue. Try using a placeholder control then adding a HtmlGenericControl to it with the markup for the embed tag i.e.
HtmlGenericControl sound = new HtmlGenericControl("<embed src=\"" + path + "\"
autostart=\"true\" hidden=\"true\"></embed>");
ucPlaceHolder.Controls.Add(sound);
Make sure the placeholder control is withing the ContentTemplate of the UpdatePanel that you are using