I bind the repeater with SqlDataReader but I want when repater has no row than a blank row is added in ItedDataBound Event
1- First Create A DataTable Object to hold your data
2- Check for the Number of Rows in the DataTable if Zero then Add Empty DataRow Object To DataTable
3- Bind Your Repeater to the DataTable Instead to Datareader Object
if(dt.Rows.Count==0)
{
DataRow dr=dt.NewRow();
dt.Rows.Add(dr);
}
rptDemo.DataSource=dt;
rptDemo.DataBind();
Try this.
Place label in the footer of repeater in FooterTemplate and by default set visible to false and in ItemDataBound set to visible=true when item count is 0 or less than 1.
<asp:Repeater ID="rptDemo" runat="server"
OnItemDataBound="rptDemo_ItemDataBound">
<ItemTemplate>
.......
</ItemTemplate>
<FooterTemplate>
<%-- Label used for no data available --%>
<asp:Label ID="lblMsg" runat="server" CssClass="errMsg" Text="Sorry, no item is there to show." Visible="false">
</asp:Label>
</FooterTemplate>
protected void rptDemo_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (rptDemo.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblFooter = (Label)e.Item.FindControl("lblMsg");
lblFooter.Visible = true;
}
}
}
Hope this helps!
My requirement was to show add row inside repeater. Me included a blank row as the last item by doing a small checking, in all other rows, blank row made hidden.
Used
<%# (((IList)((Repeater)Container.Parent).DataSource).Count).ToString() == (Container.ItemIndex + 1).ToString() %>
checking to decide whether to show or hide blank row.
Full code of view:
<table>
<asp:Repeater ID="repeater1" OnItemCommand="repeater_user_Itemcommand" runat="server">
<HeaderTemplate>
<tr>
<td>
Name
</td>
<td>
Email
</td>
<td>
Delete
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
</td>
<td>
<asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%# Eval("ID") %>'
CommandName="delete">Delete</asp:LinkButton>
</td>
</tr>
<tr id="Tr1" runat="server" visible='<%# (((IList)((Repeater)Container.Parent).DataSource).Count).ToString() == (Container.ItemIndex + 1).ToString() %>'>
<td>
<asp:TextBox ID="txtName_add" runat="server" Enabled="True" Text='' Visible="false"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtEmail_add" runat="server" Text='' Visible="false"></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="btnShowAdd" runat="server" CommandName="add">Add</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
Related
I have a problem in getting new entered record in the ListView.
In my list in ItemTemplate I replaced labels with TextBox. I have also created a button which loops over all the rows in ListView and shows the AcName field in alert message. If I update it (change the text of the AcName) to something else it still shows the record which is already present before, not the new text/value I entered. Below is the code which is working correctly for me but not generating the outcome I want, which is the new value.
HTML Markup
<asp:ListView ID="ListView1" InsertItemPosition="LastItem" runat="server" >
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="lblID" Text='<%#Eval("ID") %>'></asp:Label>
</td>
<td>
<%--<asp:Label runat="server" ID="lblAcount" Text='<%#Eval("AcName") %>'></asp:Label>--%>
<asp:TextBox ID="lblAcount" runat="server" CssClass="form-control" Text='<%#Bind("AcName") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblNaration" Text='<%#Eval("Naration") %>'></asp:Label>--%>
<asp:TextBox ID="lblNaration" runat="server" CssClass="form-control" Text='<%#Bind("Naration") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblPaidAmount" Text='<%#Eval("PaidAmount") %>'></asp:Label>--%>
<asp:TextBox ID="lblPaidAmount" runat="server" CssClass="form-control" Text='<%#Bind("PaidAmount") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblDeductAmount" Text='<%#Eval("DeductionAmount") %>'></asp:Label>--%>
<asp:TextBox ID="lblDeductAmount" runat="server" CssClass="form-control" Text='<%#Bind("DeductionAmount") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="DeleteButton" cssClass="btn btn-info fa fa-trash-o" runat="server" CommandName="DeleteIt"></asp:LinkButton>
<td>
</td>
</tr>
<asp:Panel ID="Panel2" runat="server" Visible="False">
<tr>
<td>
<asp:Label ID="Label12" runat="server" Text="Credit Acount"></asp:Label>
<asp:TextBox ID="txtAcName" runat="server" CssClass="form-control" Text='<%#Bind("AcName") %>'></asp:TextBox>
</td>
</tr>
</asp:Panel>
</ItemTemplate>
</asp:ListView>
Button handler
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For i As Integer = 0 To ListView1.Items.Count() - 1
Dim txtAcNames As TextBox = TryCast(ListView1.Items(i).FindControl("txtAcName"), TextBox)
Dim msg As String
msg = "<script language="'javascript'">"
msg += "alert('" & txtAcNames.Text & "');"
msg += "</script>"
Response.Write(msg)
Next
'txtTotal1.Text = Paid
End Sub
The above code works for me, the problem is when I enter data in the TextBox of the ListView it shows me the old values which is bound to the listview, not the new value that I enter.
what I want is it should show me the new value which I enter at runtime in the ListView TextBox.
You are alerting the txtAcName.Text at Button1_Click, but you are changing the value of lblAcount.Text at runtime. So, the old value you see is in fact from the other textbox.
Note that you have both textboxes binded to the same property <%# Bind("AcName") %>
I have an ASP.NET GridView. Now I am adding the SortExpression property tothe <TemplateField> tags, to make specific columns sortable.
Now, one of the columns has some markup content to be added in the header. The problem is, SortExpression does not work if there is a <HeaderTemplate> tag in a <TemplateField>, you have to put it inside the HeaderText property of <TemplateField>. But, all the HTML content does not work properly if I dump it inside the HeaderText property.
<asp:TemplateField SortExpression="FK_TesterID" ItemStyle-Width="300px" FooterStyle-Width="300px" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
<table width="100%">
<tr>
<td align="center">
Tester
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px" DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField = "PK_ID" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged" ToolTip="Bulk Assign Testers !" ></asp:DropDownList>
</td>
</tr>
</table>
</HeaderTemplate>
So you can see, if I put the entire <HeaderTemplate> property inside a headertext, it doesn't work.
But I want to have both functionalities. Can anyone help?
Then you need to provide a control in your HeaderTemplate with CommandName="Sort", for example a LinkButton.
<HeaderTemplate>
<table width="100%">
<tr>
<td align="center">
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort" />
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px"
DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField="PK_ID"
Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged"
ToolTip="Bulk Assign Testers !">
</asp:DropDownList>
</td>
</tr>
</table>
</HeaderTemplate>
This is a quite old thread I have stumbled over while trying to solve exactly that described problem but the solution provided here didn't worked for me. If you have a Sorting method defined for the GridView then
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort" />
will call that method
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
dt.DefaultView.Sort = e.SortExpression;
but e.SortExpression will be null and no sorting is happening. You have to first pass the Column's name through the CommandArgument of the LinkButton. Only then it worked in my case!
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" CommandArgument="ColumnName" Text="Sort" />
I have a DataGrid with 4 columns and a DataList with 5 columns. The DataList resides inside the DataGrid as another separate column by itself.
Everything is coming out fine, except that the first row in the datagrid does not display the contents of the datalist. It's as if everything in my datalist should be pushed up by a 1 row to display the data correctly.
I have validated (during debugging) that the data is coming out fine in the datalist for the corresponding first row of the datagrid, but it doesn't get rendered on the first row of the datagrid. It starts to get rendered on the second row of the datagrid.
I've been over this numerous times with the markup and the codebehind, and can't figure out why this is happening. Any help would be appreciated.
Below is my code:
HTML
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="referrals.ascx.cs" Inherits="dpbrokers.dpbrokers.referrals"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div align="center">
<asp:Label ID="errormessage" runat="server" Visible="False" /></div>
<asp:DataGrid ID="lstReferrals" runat="server" DataKeyField="ReferringAffiliateID"
AutoGenerateColumns="false" CellPadding="4" OnItemDataBound="lstReferrals_ItemDataBound">
<AlternatingItemStyle Font-Size="9pt" CssClass="small" BackColor="#C2D6FA"></AlternatingItemStyle>
<ItemStyle Font-Size="9pt" CssClass="small" BackColor="White"></ItemStyle>
<HeaderStyle Font-Size="11pt" Font-Bold="True" ForeColor="White" BackColor="#ABACAD">
</HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Referring Affiliate">
<ItemTemplate>
<asp:HyperLink Text='<%# DataBinder.Eval(Container.DataItem, "ReferringAffiliateName") %>'
NavigateUrl='<%# EditURL("Referral",DataBinder.Eval(Container.DataItem, "ReferralID").ToString(),"Edit",0) %>'
runat="server" ID="Hyperlink1" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SkyCard Awarded">
<ItemTemplate>
<asp:Label ID="itemAward" runat="server" Text='<%# IsAwarded(DataBinder.Eval(Container.DataItem, "IsAwarded").ToString()) %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="DSMAffiliateName" HeaderText="District Sales Manager">
</asp:BoundColumn>
<asp:BoundColumn DataField="DSMAffiliatePhone" HeaderText="DSM Phone"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:DataList ID="DetailList" ItemStyle-CssClass="small" DataSource="<%# riReferrals %>"
runat="server" Width="100%">
<HeaderTemplate>
<table width="100%" cellpadding="4" border="1">
<tr bgcolor="#ABACAD" class="normalwhite">
<td width="25%">
Referral Name
</td>
<td width="20%">
Phone Number
</td>
<td width="30%">
Email
</td>
<td>
Referred On
</td>
<td>
Is Member?
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="small">
<td>
<asp:HyperLink Text='<%# DataBinder.Eval(Container.DataItem, "ContactName") %>' NavigateUrl='<%# EditURL("ReferralID",DataBinder.Eval(Container.DataItem, "ReferralID").ToString(),"Edit",0) %>'
runat="server" ID="Hyperlink2" />
</td>
<td>
<%# FormatPhone(DataBinder.Eval(Container.DataItem,"Phone").ToString()) %>
</td>
<td>
<%# FormatForEmail(DataBinder.Eval(Container.DataItem, "Email").ToString())%>
</td>
<td>
(<%# DataBinder.Eval(Container.DataItem,"Created", "{0:g}") %>)
</td>
<td>
(<%# IsMember(DataBinder.Eval(Container.DataItem, "JoinedON").ToString())%>)
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
<div align="right">
<asp:ImageButton ID="Button1" CommandName="award" ImageUrl="~/images/dpbbuttons/awardbutton.gif"
AlternateText="Award SkyCard" runat="server" /></div>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
CODE BEHIND
public ArrayList riReferrals;
protected void Page_Load(System.Object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
LoadReferrals();
}
private void LoadReferrals()
{
try
{
ReferralController objReferral = new ReferralController();
lstReferrals.DataSource = objReferral.GetReferrals();
lstReferrals.DataBind();
}
catch (Exception ex)
{
Response.Write(ex);
CLogError clsLogError = new CLogError(ex, Request, Session, Context.User.Identity.Name);
clsLogError.LogError();
}
}
public void lstReferrals_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
lbl1 = (Label)e.Item.FindControl("itemAward");
GetDtlReferrals(Convert.ToInt32(lstReferrals.DataKeys[e.Item.ItemIndex]));
button.Visible = true;
if (lbl1.Text.ToLower() == "No")
button.Attributes.Add("onClick", "javascript:return confirm(\'Are You Sure You Wish To Award a SkyCard to this Agent ?\');");
else
{
button.ImageUrl = "~/images/dpbbuttons/revokebutton.gif";
button.Attributes.Add("onClick", "javascript:return confirm(\'Are You Sure You Wish To Revoke the SkyCard Award for this Agent ?\');");
}
break;
}
}
public ArrayList GetDtlReferrals(Int32 KeyField)
{
riReferrals = null;
try
{
// Obtain a list of discussion messages for the module
ReferralController objReferral = new ReferralController();
riReferrals = objReferral.GetReferralsByAgentID(KeyField);
}
catch (Exception ex)
{
Response.Write(ex);
CLogError clsLogError = new CLogError(ex, Request, Session, Context.User.Identity.Name);
clsLogError.LogError();
}
return riReferrals;
}
Got it!
In the GetDtlsReferrals method, I added the following code snippet which produced the display data I was looking for:
DataList DetailList = (DataList)e.Item.FindControl("DetailList");
DetailList.DataBind();
I have an ASP.Net Repeater I want to use to show From and Text from a dataset and I want to add it programmically. I have all the data in a dataset and can use that and I have verified the correct number for datarows when it loads so the data is their it is just not showing. What have I missed?.
Dim data As New Data
Dim ds As New DataSet
ds = data.LOADALL()
Dim drMsg() As DataRow = ds.Tables("MESSAGESYSTEM").Select("TOID='101'")
repeatMessages.DatagSource = drMsg
Now on the html side I have:
<asp:Repeater ID="repeatMessages" runat="server" >
<HeaderTemplate>
<table>
<tr>
<th>From</th>
<th>Sublect</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label1" text='<%# Eval("FROMID") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label2" text='<%# Eval("MESSAGETEXT") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
How can I fix this code to show the data in the Message table?
Try calling repeatMessages.DataBind(). All you are doing is assigning the source but you haven't told the program to do something with the data.
I'm just beinning basic data driven ASP.NET webforms design. I have the first part of a form working, but I don't know what to do next.
Please see this screenshot of what I have created so far: http://www.twitpic.com/2gnmr
I need help knowing what kind of HTML element to use in the master list, and what event/trigger on that element to use to fire off the "get child records for this selected item" sequence?
Also, is a ListView even the correct way to present the master list? At this time, I'm not trying to provide any editing features; I'll get to that later, I guess.
Should I use some other ASP.NET data control rather than hand-coding a listview like I am doing?
I don't want to see an actual "Select" link item beside each Customer Name (that looks goofy). I want the Customer Name to be the link to click on.
So, you can see in my code below that I have a ListView to present a list of CustomersWithOpenOrders. But, it's just a static list, so how do I make the Company Name label clickable, and what else will I need to make it fire back to some code-behind to fetch the child records. I already have a code-behind method to get child records for a passed-in CustomerNumber into a DataTable, and I think I would know how to bind that to a grid or listview for child records, but I do not know how to pass the CustomerNumber from the master ListView to the method from the UI form.
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table cellpadding="2" border="0" ID="tbl1" runat="server">
<tr id="Tr1" runat="server" class="lvHeader">
<th id="Th1" runat="server">Customer</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr2" runat="server">
<td>
<asp:Label ID="label2" class="FirstLine" runat="server" Text='<%# Eval("company") %>' />
<br />
<div class="SecondLine">
<asp:Label ID="labelCustNo" runat="server" Text='<%# Eval("custno") %>'/>
<asp:Label runat="server" Text='Ph: '></asp:Label>
<asp:Label ID="label3" runat="server" Text='<% # Eval("phone") %>' />
</div>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
I personally haven't found a case where the ListView can't solve my needs. If you want to create a customer selection style list you can use a link button for binding.
<asp:ListView runat="server" id="CustomersList" ItemCommand="CustomersList_ItemCommand">
<LayoutTemplate>
<table cellpadding="2" border="0" ID="tbl1" runat="server">
<tr id="Tr1" runat="server" class="lvHeader">
<th id="Th1" runat="server">Customer</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr2" runat="server">
<td>
<asp:LinkButton ID="link1" class="FirstLine" runat="server" Text='<%# Eval("company") %>' CommandName="Select" />
<br />
<div class="SecondLine">
<asp:Label ID="labelCustNo" runat="server" Text='<%# Eval("custno") %>'/>
<asp:Label runat="server" Text='Ph: '></asp:Label>
<asp:Label ID="label3" runat="server" Text='<% # Eval("phone") %>' />
</div>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:ListView runat="server" ID="OrderList">
<!-- Child Rows implementation -->
</asp:ListView>
Then you would need to bind the event to the ListView.ItemCommand.
protected void CustomersList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName = "Select")
{
if (e.Item.ItemType != ListViewItemType.DataItem) return;
var dataItem = e.Item as ListViewDataItem;
if (dataItem == null) return;
var customer = dataItem.DataItem as Customer;
if (customer == null) return;
this.OrdersList.DataSource = GetChildRecords(customer.ID);
this.OrdersList.DataBind();
}
}