I would like to disable the ListView's Select command based upon other data in the row. For example, if the UserStatus is "T", I'd like to gray out the Select hyperlink and prevent selection of that row.
I've accomplished the same thing in a GridView by the following statement in the RowCreated event. However, I haven't been able to rework that code for a ListView.
CType(e.Row.Controls(0), WebControl).Attributes("disabled") = "true"
<asp:listview runat="server" id="ListView">
<itemtemplate>
<tr id="rowUsers" runat="server">
<td><asp:linkbutton id="btnEdit" runat="server" text="Select" onclick="btnEdit_Click" /></td>
<td><asp:label id="UserNameLabel" runat="server" text='<%# Bind("UserName") %>' /></td>
<td><asp:label id="UserStatusLabel" runat="server" text='<%# Bind("UserStatus") %>' /></td>
</tr>
</itemtemplate>
Generated output...
<tr id="ListView_rowUsers_0">
<td><a id="ListView_btnEdit_0" href="javascript:__doPostBack('ListView$ctrl0$btnEdit','')">Select</a></td>
<td><span id="ListView_UserNameLabel_0">Adams,John P</span></td>
<td><span id="ListView_UserStatusLabel_0">T</span></td>
</tr>
Better use ItemDataBound event and do a FindControl("btnEdit") and simply set the Enabled property.
How to use ListView ItemDataBound event.
in your item template you can use the databinding syntax to disable the button
<ItemTemplate>
<asp:LinkButton id="btnEdit" runat="server" text="Select"
Enabled=<%# Eval("UserStatus") == "T" %> />
</ItemTemplate>
try
CType(e.Item.Controls(0), WebControl).Attributes("disabled") = "disabled"
on ListView's ItemCreated
Related
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>
I'm using Telerik's RadListBox control for my web application development, wondering how can I add row into RadListBox via TextBox control, below is my code fragment:
<telerik:RadListBox ID="rlbControl" runat="server" SelectionMode="Multiple">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' ></asp:Label>
</td>
<td style="width:20px"></td>
<td >
<asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age") %>' ></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadListBox>
Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
Age : <asp:TextBox ID="txtAge" runat="server" ></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnDel" runat="server" Text="Delete" OnClick="btnDel_Click"/>
When Add button click, get input from two TextBox and bind into RadListBox.
For deleting, select row from RadListBox and click on Delete button, remove the selected row from the RadListBox.
My question is how can I add and delete rows?
Thank you in advanced.
Hi you can create datatable and datarows. then add the textbox values to dararow. then u can bind this datatable to the RadListBox. For deleting, u need to pick the ListItem index and delete the row from the datatable and again bind to the RadListBox. It will work..
Link for delete Row demo
Also check with my answer in the below link post. you will get idea about insert row
Add rows to ListView
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 can't get <%# Eval("ID") %> work, although it works outside of the InserItemTemplate. What is worng?
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="btnInsert" runat="server" CommandName="InsertPhone" CommandArgument='<%# Eval("ID") %>' Text="InsertPhone" />
</td>
<td style="width:50px">
<asp:DropDownList runat="server" ID="ddlPhoneType" DataSourceID='ObjectDataSourcePhoneTypes'
DataTextField="Name_ar" DataValueField="ID" />
</td>
<td style="width:100px">
<asp:TextBox ID="PhoneNumberLabel" runat="server" Text='' />
</td>
</tr>
</InsertItemTemplate>
Thanks
Insert template isn't data bound so you can't use Eval there... Eval works in the context of a data bound row, but insert row is not data bound, so there's no data source for it. Programmably set the value in the field if you need to establish some value for a control.
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();
}
}