I have the following databound DataList inside an UpdatePanel which updates whenever a selection is made from a DropDown menu.
However, if nothing is retrieved from the db table, I get a blank section.
What I would like to do is display the same table which is inside the ItemTemplate along with text - i.e. 'Admin Not Found', 'Email Not Found'....etc for the EmptyDataTempate...
I've noticed that the EmptyDataTemplate doesn't exist for a DataList, but I don't want to use a different control.
Is there a way to do this? Sample code appreciated. Thanks!
<asp:DataList ID="DataList" runat="server">
<ItemTemplate>
<table>
<tr>
<td class="style1">
Company Admin:
</td>
<td>
<asp:TextBox Text='<%# Eval("CompanyAdmin") %>' CssClass="input input1" ID="co_admin"
Width="150" runat="server" ReadOnly="True" />
</td>
</tr>
<tr>
<td>
Admin Email:
</td>
<td>
<asp:TextBox Text='<%# Eval("AdminEmail") %>' CssClass="input input1" ID="ad_email"
Width="150" runat="server" ReadOnly="True" />
</td>
</tr>
<tr>
<td>
Company Email:
</td>
<td>
<asp:TextBox Text='<%# Eval("CompanyEmail") %>' CssClass="input input1" ID="co_email"
Width="150" runat="server" ReadOnly="True" />
</td>
</tr>
<tr>
<td>
Telephone:
</td>
<td>
<asp:TextBox Text='<%# Eval("Telephone") %>' CssClass="input input1" ID="telephone"
Width="150" runat="server" ReadOnly="True" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
You can add a simple function that return a msg if the field is null.
You call a function like
<%#GetWithMsg(Container.DataItem, "CompanyEmail", "e-mail n/a")%>
for example
<asp:TextBox Text='<%# GetWithMsg(Container.DataItem, "CompanyEmail", "e-mail n/a") %>' CssClass="input input1" ID="co_email"
Width="150" runat="server" ReadOnly="True" />
and the code behind.
protected string GetWithMsg(object oItem, string cField, string TheMsg)
{
var TheData = DataBinder.Eval(oItem, cField);
if(TheData == null)
return TheMsg;
else
return TheData.ToString();
}
not tested but mybe you can try
'<%# !string.IsNullOrEmpty(Eval("CompanyEmail").ToString()) ? Eval("CompanyEmail") : "Email Not Found" %>'
explicit casting might be better in terms of performance than eval though
e.g.
'<%# !string.IsNullOrEmpty(((System.Data.DataRowView)Container.DataItem)["CompanyEmail"].ToString()) ? ((System.Data.DataRowView)Container.DataItem)["CompanyEmail"].ToString() : "Email Not Found" %>'
hope itll help
Sample Edit:
protected void BindData()
{
DataTable dt = DAL.GetData(...
if(dt.Rows.Count == 0)
{
dt.Rows.Add(dt.NewRow());
}
DataList.DataSource = dt;
DataList.DataBind();
}
Related
I am populating a Gridview using a form with the help of AJAX.
I want to add a delete button to each row and whendelet button is clicked that row should be deleted from GridView (not hidden).
Here is my aspx code:
<asp:Content ID="Content2" ContentPlaceHolderID="Nav" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<table>
<tr>
<td class="auto-style1">
<asp:Label ID="LabelFirstName" runat="server" Text="First Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBoxFirstName" runat="server" CssClass="auto-style2"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="LabelLastName" runat="server" Text="Last Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBoxLastName" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<br /><br />
<asp:Button ID="ButtonSearch" runat="server" Text="Search" CssClass="auto-style3" Width="93px" OnClick="ButtonSearch_Click" />
<br /><br />
<asp:Label ID="LabelNoRows" runat="server" Text="Sorry, we couldn't find any data with this Name." Visible="false" ForeColor="Red"></asp:Label>
<asp:Panel ID="PanelAbsenceInfo" runat="server" Visible="false">
<table>
<tr>
<td class="auto-style4">
<asp:Label ID="LabelFname" runat="server" Text="First Name:"></asp:Label>
</td>
<td>
<asp:Label ID="LabelGetFirstName" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="LabelLname" runat="server" Text="Last Name"></asp:Label>
</td>
<td>
<asp:Label ID="LabelGetLname" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="LabelEmail" runat="server" Text="Email"></asp:Label>
</td>
<td>
<asp:Label ID="LabelGetEmail" runat="server"></asp:Label>
</td>
</tr>
<asp:UpdatePanel ID="UpdatePanelInfo" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<tr>
<td class="auto-style4">
<asp:Label ID="LabelPersonal" runat="server" Text="Personal Days Approved" ></asp:Label>
</td>
<td>
<asp:Label ID="LabelGetPersonal" runat="server" ></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="LabelOther" runat="server" Text="Other Days Approved"></asp:Label>
</td>
<td>
<asp:Label ID="LabelGetOther" runat="server" ></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="LabelTotalDays" runat="server" Text="Total Days Approved" ></asp:Label>
</td>
<td>
<asp:Label ID="LabelgetTotaldays" runat="server" ></asp:Label>
</td>
</tr>
</table>
<br /><br />
<asp:GridView ID="GridViewViewAllRequests" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
OnRowCommand="GridViewViewAllRequests_RowCommand">
<Columns>
<asp:BoundField DataField="scrap" Visible ="false" />
<asp:BoundField DataField="isApproved" Visible="false" />
<asp:BoundField HeaderText="Date of Absence" DataField="requestedDate" DataFormatString="{0:MM/dd/yyyy}" />
<asp:BoundField HeaderText="Rotation Period" DataField="rotationPeriod" />
<asp:BoundField HeaderText="Reason" DataField="reason" />
<asp:BoundField HeaderText="Days Missed" DataField="daysMissed" />
<asp:BoundField HeaderText="Department" DataField="departmentName" />
<asp:BoundField HeaderText="Course" DataField="courseName" />
<asp:TemplateField HeaderText="Approved/Declined">
<ItemTemplate>
<asp:Label ID="LabelApproveorDecline" runat="server" Text='<%# Eval("isApproved") == null ? "Decision not yet made." : ((bool)Eval("isApproved") ? "Approved" : "Declined") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Scrap/Undo">
<ItemTemplate>
<asp:Button ID="ScrapButton" CommandArgument='<%# Eval("id") %>' runat="server" Text="Scrap" CommandName="Scrap" Visible='<%# !(bool)Eval("scrap") %>' />
<asp:Button ID="UndoButton" CommandArgument='<%# Eval("id") %>' runat="server" Text="Undo" CommandName="Undo" Visible='<%# (bool)Eval("scrap") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<br /><br />
</asp:Content>
And here is my cs code:
public partial class AbsenceRequestMonitor : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonSearch_Click(object sender, EventArgs e)
{
DoAction();
}
public Boolean ScraptheRequest(int id, bool action)
{
bool success = false;
success = DBOperation.ScrapAbsencedate(id, action);
return success;
}
protected void GridViewViewAllRequests_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Determine the RowIndex of the Row whose Button was clicked.
int id = Convert.ToInt32(e.CommandArgument);
//Get the value of column from the DataKeys using the RowIndex.
// int id = Convert.ToInt32(GridViewViewAllRequests.DataKeys[rowIndex].Values[0]);
if(e.CommandName == "Scrap")
{
ScraptheRequest(id, true);
}
else if(e.CommandName == "Undo")
{
ScraptheRequest(id, false);
}
DoAction();
UpdatePanelInfo.Update();
}
public void DoAction()
{
string firstName = null;
string lastName = null;
firstName = TextBoxFirstName.Text.ToString();
lastName = TextBoxLastName.Text.ToString();
double PdCount = 0;
double OtherCount = 0;
if (firstName != null && lastName != null)
{
//gets student data from the Student Form
List<AbsenceMonitorData> l_studentAbsenceInfo = DBOperation.getStudentAbsenceInfo(firstName, lastName);
AbsenceMonitorData studentAbsenceInfo = l_studentAbsenceInfo.FirstOrDefault();
PdCount += l_studentAbsenceInfo.Where(x => x.reason == "Personal Day").Where(y => y.isApproved == true).Where(z => z.scrap == false).Select(a => a.daysMissed).Sum();
OtherCount += l_studentAbsenceInfo.Where(x => x.reason != "Personal Day").Where(y => y.isApproved == true).Where(z => z.scrap == false).Select(a => a.daysMissed).Sum();
GridViewViewAllRequests.DataSource = l_studentAbsenceInfo;
GridViewViewAllRequests.DataBind();
if (l_studentAbsenceInfo.Count > 0)
{
LabelGetPersonal.Text = PdCount.ToString();
LabelGetOther.Text = OtherCount.ToString();
LabelgetTotaldays.Text = (PdCount + OtherCount).ToString();
LabelGetFirstName.Text = studentAbsenceInfo.firstName.ToString();
LabelGetLname.Text = studentAbsenceInfo.lastname.ToString();
LabelGetEmail.Text = studentAbsenceInfo.studentEmail.ToString();
PanelAbsenceInfo.Visible = true;
LabelNoRows.Visible = false;
}
else
{
LabelNoRows.Visible = true;
}
}
}
}
Right now i was using the Scrap/Undo button which i want to replace it with a Delete button.
Also when i delete the row the count should be affected. (ie. Personal Days Approved, Other Days Approved and Total Days Approved)
The image shows that when i type the name and hit search, GridView is obtained.
I need to be able to delete rows from that gridview.
GridView
I have a html table on a page:
<table id="tblMain" runat="server" style="margin-left: auto; margin-right: auto;">
<tr>
<td>
Safety
</td>
<td>
<asp:TextBox ID="txtSafety" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSafety" runat="server" Width="150px" Text="Edit"
OnClick="Edit_Text" CommandArgument="0" />
</td>
</tr>
<tr>
<td>
Environment
</td>
<td>
<asp:TextBox ID="txtEnvironment" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="bntEnvironment" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="1" />
</td>
</tr>
<tr>
<td>
Quality
</td>
<td>
<asp:TextBox ID="txtQuality" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnQuality" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="2" />
</td>
</tr>
<tr>
<td>
Ferrous System
</td>
<td>
<asp:TextBox ID="txtFerrousSystem" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnFerrousSystem" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="3" />
</td>
</tr>
<tr>
<td>
Coke System
</td>
<td>
<asp:TextBox ID="txtCokeSystem" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnCokeSystem" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="4" />
</td>
</tr>
<tr>
<td>
Coal Yards
</td>
<td>
<asp:TextBox ID="txtCoalYards" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnCoalYards" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="5" />
</td>
</tr>
<tr>
<td>
Screenhouse
</td>
<td>
<asp:TextBox ID="txtScreenhouse" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnScreenhouse" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="6" />
</td>
</tr>
<tr>
<td>
Process Plant
</td>
<td>
<asp:TextBox ID="txtProcessPlant" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnProcessPlant" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="7" />
</td>
</tr>
<tr>
<td>
New Mill
</td>
<td>
<asp:TextBox ID="txtNewMill" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnNewMill" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="8" />
</td>
</tr>
<tr>
<td>
Streamphases
</td>
<td>
<asp:TextBox ID="txtStreamphases" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnStreamphases" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="9" />
</td>
</tr>
<tr>
<td>
Furnace Silos Injection
</td>
<td>
<asp:TextBox ID="txtFurnaceSilosInjection" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnFurnaceSilosInjection" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="10" />
</td>
</tr>
<tr>
<td>
CompressedAir
</td>
<td>
<asp:TextBox ID="txtCompressedAir" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnCompressedAir" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="11" />
</td>
</tr>
<tr>
<td>
Planned Maintenance
</td>
<td>
<asp:TextBox ID="txtPlannedMaintenance" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnPlannedMaintenance" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="12" />
</td>
</tr>
<tr>
<td>
Notifications Raised
</td>
<td>
<asp:TextBox ID="txtNotificationsRaised" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnNotificationsRaised" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="13" />
</td>
</tr>
<tr>
<td>
Manning
</td>
<td>
<asp:TextBox ID="txtManning" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnManning" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="14" />
</td>
</tr>
<tr>
<td>
ShiftHandover
</td>
<td>
<asp:TextBox ID="txtShiftHandover" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server" OnClick="Edit_Text"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnShiftHandover" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="15" />
</td>
</tr>
</table>
When the button is pressed the following method is called:
protected void Edit_Text(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
{
editField = btn.ID.Replace("btn", "");
btn.Visible = false;
//Getting and storing rowindex
TextBox txt = this.FindControl("txt" + editField) as TextBox;
txt.ReadOnly = false;
txt.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFB2");
txt.Focus();
comment = txt.Text;
//Further methods unnecessary for this question
}
}
As you can see, the prefix is stripped from the controls and then added in when necessary to find the right control at the point, therefore the string for the FindControl method is correct and should return a a TextBox.
However, by stepping through I can see that it is passing an empty TextBox instance rather than the expected result or a null.
I have already tried the findcontrol function on the table itself, I used 'this' as a second check but both yielded the same results.
I suspect the html table is a problem as in another method I am having issues finding a button manually in the html table:
Button editbtn = tblMain.Rows[GetRowIndex(hdnRowIndex.Value)].Cells[2].Controls[0] as Button;
Which is returning a null despite there always being a cell button in that cell for each row
EDIT - Here is the binding that is performed
private void bindData(DataTable dt)
{
if (dt.Rows.Count > 0)
{
foreach (DataColumn col in dt.Columns)
{
TextBox txt = tblMain.FindControl("txt" + col.ColumnName) as TextBox;
if (txt != null)
{
txt.Text = dt.Rows[0][col].ToString();
}
}
}
}
Your problem is here:
this.FindControl("txt" + editField) as TextBox;
FindControl is not recursive and can only find immediate children of the control it was called on. Here you are calling it of the page instance, so it only seeks through immediate children of the page in the control tree (presumably this is only a form element).
What you need to do is to pick a server-side control right above your textboxes/buttons and run FindControl of it. For instance, let's say it is your table, which is declared as:
<table>
<%--...all the content from the post goes here...--%>
</table>
Make this table server-side:
<table id="TheTable" runat="server">
and then call FindControl on this table:
TextBox txt = TheTable.FindControl("txt" + editField) as TextBox;
You can of course keep the table client-side, and use some control that wraps this table. Just make sure that in between your control and the textbox in question there are no other server-side controls.
<table style="width:100%; margin-left:auto; margin-right:auto;" id="tblTst" runat="server">
<tr>
<td>
<h2>
Vehicle Information
<asp:Label ID="lblTest" Text="THis is a Test" ForeColor="Salmon" runat="server" />
<asp:Button ID="btnTest" CommandArgument="0" OnClick="btnTest_click" runat="server" Text="Test" />
</h2>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblTst2" Text="This is also a Test" ForeColor="Salmon" runat="server" />
<asp:Button ID="btnTest2" CommandArgument="1" OnClick="btnTest_click" runat="server" Text="Test" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMsg" runat="server" />
</td>
</tr>
</table>
protected void btnTest_click(object sender, EventArgs e)
{
Button btn = sender as Button;
string comment = "";
string editField = "";
if (btn != null)
{
editField = btn.ID.Replace("btn", "");
btn.Visible = false;
//Getting and storing rowindex
foreach(HtmlTableCell cl in tblTst.Rows[Convert.ToInt32(btn.CommandArgument)].Cells)
{
foreach (Control ctrl in cl.Controls)
{
if(ctrl is Label)
{
Label txt = new Label();
txt = ctrl as Label;
txt.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFB2");
txt.Focus();
comment = txt.Text;
lblMsg.Text = comment;
return;
}
}
}
//Further methods unnecessary for this question
}
}
}
This is what I came up with, this does work; however it is a fairly dirty way to do this, I'm sure you can clean it up quite a bit. This is only intended as a starting point. The big different is that you need to look in the cell for the control not in the table or page, also the cell is an htmltable cell not just a table cell.
Please can anyone suggest a way to databind a database table to a gridview but in a specific layout where column values should appear in sub rows within a main row, like wise all rows from the database table should list down inside that Gridview.I know the normal way of databinding to a gridview Where it display row data(From Database Table) as columns, but what I want is like below
Gridview is not suitable for this type of format.
If you are okay with repeater, you can do something like this:
And here's a link to MSDN: Repeater Class.
UPDATE: If you want to post each answer selection, you can use option button and group them. You can use questionid as a part of group name and in the code get the question id. Your markup may look like below:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th colspan="2"></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "QuestionId") %> </td>
<td>
<table>
<tr>
<td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Question") %> </td>
</tr>
<tr>
<td>
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer1") %>'
OnCheckedChanged="RadioButton1_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer2") %>'
OnCheckedChanged="RadioButton2_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer3") %>'
OnCheckedChanged="RadioButton3_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer4") %>'
OnCheckedChanged="RadioButton4_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And in the code:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
string grpId = ((RadioButton)sender).GroupName;
int questionId = 0;
int.TryParse(grpId.Split('_')[1].ToString(), out questionId);
//Use questionId
}
I am having difficulties hiding the sorting arrows on a column header that is not sortable.
I clearly define my sortable columns in the HeaderTemplate sections, and non sortable columns in the ItemTemplate, yet the sorting arrows still appears for the second TemplateField defined below. What am i Missing?
<asp:GridView ID="gvBeneficiary" runat="server" Width="100%" AllowPaging="True" CssClass="gridheader"
EmptyDataText=""
AutoGenerateColumns="False" PageSize="10"
OnPageIndexChanging="gvBeneficiary_PageIndexChanging" OnRowCommand="gvBeneficiary_RowCommand"
OnRowDataBound="gvBeneficiary_RowDataBound" HeaderStyle-CssClass="lhs">
<Columns>
<asp:TemplateField HeaderStyle-CssClass="lhs">
<HeaderTemplate>
<asp:LinkButton ID="lnkFullName" Text="Full Name"
CommandName="Sort" CommandArgument="FullName" runat="server"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<%#Eval("FullName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="EditClick" CommandArgument='<%#Eval("RecipientID") %>'
Text="Edit">
</asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="DeleteClick" CommandArgument='<%#Eval("RecipientID") %>'
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
What do u mean by defining non sortable columns in the ItemTemplate ?
Specify Allowsorting =true for the grid asd sort expression for respective columns which u want sorting to be enabled.
<asp:GridView AllowSorting="true"......../> and
<asp:TemplateField.............. SortExpression ="Full Name">
And don't specify the sort expression for columns u don't need sorting.
I think this is enough for sorting specified columns.
Is there a reason you can't use the default sorting ability in the gridview? If you can, apply the "arrows" CSS to all hyperlinks in a table header cell
.gvclass th a {background-image...}
That way all headers with a SortExpression will have those CSS properties and all non-sortable headers will not (as no hyperlink will be generated in those cells).
If you just want to disable sorting for a specific column in an event, normally it should work to set the GridViewColumn's SortExpression to null:
gvBeneficiary.Columns[0].SortExpression = null;
I have done this using listview.Please try it with Gridview
my html code is
<asp:ListView ID="lst_Area" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_Area_ItemDataBound">
<LayoutTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td width="35px" align="left">
S.No
</td>
<td align="left" width="400px">
<asp:LinkButton ID="lnk_Name" runat="server" CommandArgument="tblAreaNew.AreaName"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Name</asp:LinkButton>
<asp:Image ID="img_lnk_Name" Visible="false" runat="server" />
</td>
<td align="left" width="250px">
<asp:LinkButton ID="lnk_Location" runat="server" CommandArgument="tblAreaNew.Locationid"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Location</asp:LinkButton>
<asp:Image ID="img_lnk_Location" Visible="false" runat="server" />
</td>
<td align="left" width="175px">
<asp:LinkButton ID="lnk_CreatedBy" runat="server" CommandArgument="v.FirstName" ValidationGroup="vgSearch"
OnClick="lnk_Sort">Created By</asp:LinkButton>
<asp:Image ID="img_lnk_CreatedBy" Visible="false" runat="server" />
</td>
<td align="left" width="120px">
<asp:LinkButton ID="lnk_CreatedOn" runat="server" CommandArgument="tblAreaNew.createddate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Created On</asp:LinkButton>
<asp:Image ID="img_lnk_CreatedOn" Visible="false" runat="server" />
</td>
<td align="left" width="175px">
<%--<asp:LinkButton ID="lnkCreatedDate" runat="server" CommandArgument="tblUserActivities.CreatedDate"
OnClick="lnk_Sort">Created Date</asp:LinkButton>--%>
<asp:LinkButton ID="lnk_LastModfiedBy" runat="server" CommandArgument="v.FirstName"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified By</asp:LinkButton>
<asp:Image ID="img_lnk_LastModfiedBy" Visible="false" runat="server" />
</td>
<td align="left" width="120px">
<asp:LinkButton ID="lnk_LastModfiedOn" runat="server" CommandArgument="tblAreaNew.ModifiedDate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified On</asp:LinkButton>
<asp:Image ID="img_lnk_LastModfiedOn" Visible="false" runat="server" />
</td>
<td align="center" width="50px">
<asp:LinkButton ID="lnk_Status" runat="server" CommandArgument="tblAreaNew.isactive"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Status</asp:LinkButton>
<asp:Image ID="img_lnk_Status" Visible="false" runat="server" />
</td>
<td align="center" width="50px" style="border-right: 1px solid #6398cc;">
Activity
<%-- <div style="width: 50px; float: right;">
</div>--%>
</td>
</tr>
<tr id="tr" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="left" valign="middle">
<%# Container.DataItemIndex+1 %>.
</td>
<td align="left">
<asp:Label ID="lblAreaID" runat="server" Text='<%# Eval("Areaid")%>' Visible="false"></asp:Label>
<%# Eval("AreaName")%>
</td>
<td align="left">
<%# Eval("Location")%>
</td>
<td align="left">
<%# Eval("CreatedBy")%>
</td>
<td align="left">
<%# Convert.ToDateTime(Eval("CreatedDate")).ToString("MMM, dd yyyy")%>
</td>
<td align="left">
<%# Eval("ModifiedBy")%>
</td>
<td align="left">
<%# Convert.ToDateTime(Eval("ModifiedDate")).ToString("MMM, dd yyyy")%>
</td>
<td align="center">
<asp:Label ID="lblLocationIsActive" runat="server" Style="display: none;" Text='<%# Eval("LocationIsActive")%>'></asp:Label>
<asp:Label ID="lbl_Status" runat="server" Style="display: none;" Text='<%# Eval("IsActive")%>'></asp:Label>
<asp:ImageButton ID="imgbtnStatus" runat="server" CommandArgument='<%# Eval("Areaid") %>'
OnClick="imgbtnStatus_Onclick" />
</td>
<td class="last" align="center">
<asp:Label ID="lblAreaName" runat="server" Style="display: none;" Text='<%# Eval("AreaName")%>'></asp:Label>
<asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/edit.png"
ToolTip="Edit Details" CommandArgument='<%# Eval("AreaId") %>' OnClick="imgbtnEdit_OnClick" />
<asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/delete.png"
ToolTip="Delete" CommandArgument='<%# Eval("AreaId") %>' Visible="false" OnClientClick="return confirm('Are you sure you want to delete the location?');"
OnClick="imgbtnDelete_OnClick" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
And my code behind code is
protected void lnk_Sort(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
string arg = lnk.CommandArgument.ToString();
ViewState["sortCol"] = arg;
GetSortDirection();
BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), Pager.PageSize);
string name = lnk.ID;
Image img = (Image)(lst_Area.FindControl("img_" + name));
if (img != null)
{
SetSortOrderImage(img, ViewState["sortDir"].ToString());
}
}
private void SetSortOrderImage(Image image, String sortorder)
{
if (sortorder == "asc")
{
image.Visible = true;
image.ImageUrl = "../App_Themes/ThemeNew2/images/up.png";
}
else if (sortorder == "Desc")
{
image.Visible = true;
image.ImageUrl = "../App_Themes/ThemeNew2/images/down.png";
}
}
/// <summary>
/// this method get the sort direction
/// </summary>
private void GetSortDirection()
{
if (Convert.ToString(ViewState["sortDir"]) == "Desc")
{
ViewState["sortDir"] = "asc";
}
else
{
ViewState["sortDir"] = "Desc";
}
}
Hope this will work for you.one this more i would like to let you know the inbuilt sorting and paging are slower for gridview as compare to custom paging and sorting.
I got a table with 2 nested tables inside that display my two repeaters. My repeaters display Home and office addresses respectively. The problem is that whenever I add a new record in one repeater, the other repeater table's display gets messed up. Like if I add a record in rpt1 , then table for rpt1 moves up and table for rpt2 goes down..ie the headers do not display in one single line . They move up and down when records are added or deleted. WHat I want is these headers to be fixed so if I add new records or delete records, the headings of both repeaters display on the same line...How do I fix this ? Hope its not confusing.
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="50%">
<asp:Panel ID="pnlAddAddress" runat="server">
<asp:Repeater ID="rpt1" OnItemCommand="rpt1_ItemCommand" runat="server" OnItemDataBound="rpt1_OnItemDataBound">
<HeaderTemplate>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="5" class="linegrey">
</td>
</tr>
<tr class="lgrey">
<td>
Address1
</td>
<td>
City
</td>
<td>
State
</td>
<td>
IsDefault
</td>
<td>
Actions
</td>
</tr>
<tr>
<td colspan="5" class="dots">
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkAddressB" runat="server" Text='<%# Eval("Address1")%>' CommandName="DisplayAddressB" CommandArgument='<%#Eval("AddID") %>' CausesValidation=false></asp:LinkButton>
</td>
<td>
<%# Eval("City")%>
</td>
<td>
<%# Eval("State")%>
</td>
<td>
<%-- Visible='<%# Eval("IsDefault")%>'--%>
<asp:LinkButton ID="lnkDefaultB" Text="Set as Default" CommandName="SetDefaultB" runat="server" CommandArgument='<%# Eval("AddID") + "," + Eval("IsB") %>'
CausesValidation="false" Visible='<%# Eval("IsDefault")%>'></asp:LinkButton>
<asp:Label ID="labelDefaultB" Text="Yes" runat="server" Visible='<%# Eval("IsDefault")%>'></asp:Label>
</td>
<td>
<asp:ImageButton ID="lnkAdd" CommandArgument='<%#Eval("AddID") %>'
CausesValidation="false" CommandName="Edit" runat="server" ImageUrl="~/images/Edit.gif" Width="14" Height="14" ToolTip="Edit"></asp:ImageButton>
<asp:ImageButton ID="lnkDel" Text="Delete" CommandArgument='<%#Eval("AddID") %>'
CausesValidation="false" CommandName="Delete" runat="server" ImageUrl="~/images/Delete.gif" Width="14" Height="14" ToolTip="Delete"></asp:ImageButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>
</td>
<td>
<asp:Panel ID="pnlSping" runat="server">
<asp:Repeater ID="rpt12" OnItemCommand="rpt12_ItemCommand" runat="server" OnItemDataBound="rptSpping_OnItemDataBound">
<HeaderTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="5" class="linegrey">
</td>
</tr>
<tr class="lgrey">
<td>
Address1
</td>
<td>
City
</td>
<td>
State
</td>
<td>
IsDefault
</td>
<td>
Actions
</td>
</tr>
<tr>
<td colspan="5" class="dots">
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkAddressS" runat="server" Text='<%# Eval("Address1")%>' CommandArgument='<%#Eval("AddID") %>' CommandName="DisplayAddressS" CausesValidation="false"></asp:LinkButton>
</td>
<td>
<%# Eval("City")%>
</td>
<td>
<%# Eval("State")%>
</td>
<td>
<asp:LinkButton ID="lnkDefaultS" Text="Set as Default" CommandName="SetDefaultS" runat="server" Visible=true CommandArgument='<%# Eval("AddID") + "," + Eval("IsS") %>'
CausesValidation="false"></asp:LinkButton>
<asp:Label ID="labelDefaultS" Text="Yes" runat="server" Visible=true></asp:Label>
</td>
<td>
<asp:ImageButton ID="lnkAdd" Text="Edit" CommandArgument='<%#Eval("AddID") %>'
CausesValidation="false" CommandName="Edit" runat="server" ImageUrl="~/images/Edit.gif" Width="14" Height="14" ToolTip="Edit"></asp:ImageButton>
<asp:ImageButton ID="lnkDel" Text="Delete" CommandArgument='<%#Eval("AddID") %>'
CausesValidation="false" CommandName="Delete" runat="server" ImageUrl="~/images/Delete.gif" Width="14" Height="14" ToolTip="Delete"></asp:ImageButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>
</td>
</tr>
</table>
Use css attribute valign and set its value to top like this <td width="50%" valign="top">.
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="50%" valign="top">
<asp:Panel ID="pnlAddAddress" runat="server">
......
</asp:Panel>
</td>
<td valign="top">
<asp:Panel ID="pnlSping" runat="server">
......
</asp:Panel>
</td>
</tr>
</table>
For headers look to use the <th> element, this will keep them at the top of the table.
Put the <table> tags you do have outside your repeater controls, make sure the repeater only renders a single row and the same for the header.
Unless you have good reason consider just wrapping the two tables in <div> tags as nested tables really don't work ideally for layout.