I'm an asp.net newbie. Basically, I have a DropDownList within and EditItemTemplate within a ListView. When a new item is selected in the drop down, the user would like to NOT have to click the LinkButton for update, but have the update happen automatically. I've experimented with the OnSelectedIndexChanged="ddlrank_itemChanged" using code behind, as well as OnChange="MyFoo()" in javascript, but the details of what to do are beyound me.
I hope I am including the code sample correctly. Any suggestions will be greatly appreciated. Thanks.
<asp:ListView ID="ListView1" runat="server" DataKeyNames="rankingID" DataSourceID="SqlDataSource1" OnItemUpdated="ListView1_Item_Updated">
<LayoutTemplate>
<table cellpadding="2" width="640px" border="1" runat="server" id="tblRankings">
<tr id="Tr1" runat="server">
<th id="Th1" runat="server">Action</th>
<th id="Th3" runat="server">Rank</th>
<th id="Th4" runat="server">Committee name</th>
<th id="Th5" runat="server">Committee type</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="RankingDataPager" PageSize="100">
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate >
<tr id="Tr2" runat="server">
<td>
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" />
</td>
<td valign="top">
<asp:Label ID="RankLabel" runat="Server" Text='<%#Eval("rank") %>' />
</td>
<td valign="top">
<asp:Label ID="CommitteeNameLabel" runat="Server" Text='<%#Eval("committeename") %>' />
</td>
<td valign="top">
<asp:Label ID="CommitteeTypeLabel" runat="Server" Text='<%#Eval("committeetype") %>' />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate >
<tr style="background-color: #ADD8E6">
<td>
<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:DropDownList ID="ddlrank"
DataSourceID="sdsrank"
DataValueField="vchvalue"
DataTextField="vchvalue"
OnSelectedIndexChanged="ddlrank_itemChanged"
OnChange="MyFoo()"
SelectedValue='<%# Bind("rank") %>' runat="server" >
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="CommitteeNameTextBox" runat="server" Enabled="false" ReadOnly="true" Text='<%#Bind("committeename") %>'
MaxLength="200" /><br />
</td>
<td>
<asp:TextBox ID="CommitteeTypeTextBox" runat="server" Enabled="false" ReadOnly="true" Text='<%#Bind("committeetype") %>'
MaxLength="20" /><br />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
I think you are missing AutoPostBack="true" on your dropdownlist property. Hope it helps.
You're headed in the right direction. You can use "OnSelectedIndexChanged", or you can create a function in your codebehind page that handles the SelectedIndexChanged event. Either way, when the user makes a selection from the dropdown, you get a postback and that function is executed. In the function you can do whatever you want. In a case like this that might mean checking what the selected value is and setting other values on the screen based on the new selection. When the function exits, a new screen is sent to the user's browser with any updated data.
Thanks for the replies, they helped direct and refine my internet searching!
I finally found a simple solution.
It just requires two lines of code in the OnSelectedIndexChanged code-behind function defined on the dropdownlist:
protected void ddlrank_itemChanged(object sender, EventArgs e)
{
ListViewDataItem item = (ListViewDataItem)((DropDownList)sender).Parent;
ListView1.UpdateItem(item.DisplayIndex, false);
}
Related
I am facing problems while using the ListView Control in asp.net
I used two buttons links in a ListView ItemTemplate. For both buttons, I used Command Name and Command Argument. But first one is working fine and the second one is giving errors. I.e.
System.InvalidOperationException: Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert.
If I want to add the InsertTemplate, where do we have to place it?
I am copying my code. Please help me.
Design View :
<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" GroupItemCount="2" OnPagePropertiesChanging="ListView1_PagePropertiesChanging" DataKeyNames="InventoryID" OnItemCommand="ListView1_ItemCommand">
<LayoutTemplate>
<table width="100%">
<tr style="background-color:lightblue;color:blue;text-align:center;font-size:25px;font-weight:bold">
<td colspan="2">Available Books</td>
</tr>
<asp:PlaceHolder ID="groupPlaceHolder1" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<table cellpadding="2" cellspacing="0" border="1" style="width:100%;height:100px; border:dashed 1px #04AFEF;background-color:#B0E2F5">
<tr>
<td>
<asp:Button ID="btnReview" runat="server" Text="Review" CommandName="Select" CommandArgument='<%# Eval("InventoryID") %>'/>
</td>
<td></td>
<td>
<asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="Insert" CommandArgument='<%# Eval("InventoryID") %>' />
</td>
</tr>
</table>
</td>
</ItemTemplate>
</asp:ListView>
Well you can insert a InsertItemTemplate like here :
<InsertItemTemplate>
<tr style="background-color:#D3D3D3">
<td valign="top">
<asp:Label runat="server" ID="FirstNameLabel"
AssociatedControlID="FirstNameTextBox" Text="First Name"/>
<asp:TextBox ID="FirstNameTextBox" runat="server"
Text='<%#Bind("FirstName") %>' /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%#Bind("LastName") %>' /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="E-mail" />
<asp:TextBox ID="EmailTextBox" runat="server"
Text='<%#Bind("EmailAddress") %>' />
</td>
<td>
<asp:LinkButton ID="InsertButton" runat="server"
CommandName="Insert" Text="Insert" />
</td>
</tr>
</InsertItemTemplate>
Used From : https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.insertitemtemplate(v=vs.110).aspx
This is my first post, so please be gentle. With regard to the markup and code below (this is all inside a user control), this works fine to begin with. The problem occurs when I try to delete more than one item from the list that the formview renders.
I can delete an item and the page reloads and the item that I deleted is gone. If I then click delete on another item there is no apparent response at all. Putting breakpoints on all of the functions etc in the page behind shows that it does actually go through everything that it should do, but the browser shows no response at all. If I then manually refresh the browser the second item that I have deleted has gone.
I really need to get the browser to refresh every time I click delete and if possible I could do with understanding the problem please. I have tried using ViewStateMode="Disabled/Enabled" in both the formview and the repeater separately and together.
Additional - I have also tried disabling caching.
Thanks in advance.
<asp:FormView ID="fvDrugActiveSubstance" runat="server" DefaultMode="ReadOnly" UseSubmitBehavior="false" Width="100%">
<ItemTemplate>
<table style="border-collapse: collapse; border-spacing: 0;width: 100%;">
<tr>
<th style="width:80%;">Name</th>
<th style="width:10%;">Edit</th>
<th style="width:10%;">Delete</th>
</tr>
<asp:Repeater ID="rptrDrugActiveSubstance" runat="server" OnItemCommand="rptrDrugActiveSubstance_ItemCommand">
<ItemTemplate>
<tr class="view_table" style="">
<td>
<asp:Label Text='<%# Eval("ActiveSubstanceName") %>' ID="lblName" runat="server" />
<asp:Label Text='<%# Eval("DrugSafetyDrugCode") %>' ID="lblCode" runat="server" Visible="false" />
</td>
<td>
<img onclick='<%# String.Format("openDrugActiveSubstanceWindow( {1},{0},\"Write\" );return false;",((DrugActiveSubstance)Container.DataItem).DrugSafetyDrugCode.ToString(),((DrugActiveSubstance)Container.DataItem).DrugSafetyDrugActiveSubstanceCode) %>' src="/images/grid_icons/edit.png" style="cursor: pointer;" title="Edit" />
</td>
<td>
<asp:ImageButton ID="btnDelete" runat="server" CommandArgument='<%# Eval("DrugSafetyDrugActiveSubstanceCode") %>' style="cursor: pointer;" CommandName="cmd_delete" ImageUrl="~/images/grid_icons/Delete.gif" ToolTip="Delete" />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="view_table_alt">
<td>
<asp:Label Text='<%# Eval("ActiveSubstanceName") %>' ID="lblName" runat="server" />
<asp:Label Text='<%# Eval("DrugSafetyDrugCode") %>' ID="lblCode" runat="server" Visible="false" />
</td>
<td>
<img onclick='<%# String.Format("openDrugActiveSubstanceWindow( {1},{0},\"Write\" );return false;",((DrugActiveSubstance)Container.DataItem).DrugSafetyDrugCode.ToString(),((DrugActiveSubstance)Container.DataItem).DrugSafetyDrugActiveSubstanceCode) %>' src="/images/grid_icons/edit.png" style="cursor: pointer;" title="Edit" />
</td>
<td id="tdDelete">
<asp:ImageButton ID="btnDelete" runat="server" CommandArgument='<%# Eval("DrugSafetyDrugActiveSubstanceCode") %>' style="cursor: pointer;" CommandName="cmd_delete" ImageUrl="~/images/grid_icons/Delete.gif" ToolTip="Delete" />
</td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
</table>
</ItemTemplate>
</asp:FormView>
protected void rptrDrugActiveSubstance_ItemCommand(object source, RepeaterCommandEventArgs e)
{
drugSafetyService.DeleteDrugActiveSubstance(int.Parse(e.CommandArgument.ToString()));
Response.Redirect(HttpContext.Current.Request.Url.ToString().Split('?')[0] + "?#drugAS_accordian");
}
I'm going to chalk this one up as experience. Having found a javascript function that was causing the abort the problem is now resolved.
I am using ASP.NET 4.5 Model Binding to present items in a ListView control with editing.
<asp:ListView ID="Results" runat="server" SelectMethod="SelectClientStatus" DataKeyNames="ID" ItemPlaceholderID="itemPlaceHolder" ItemType="ClientStatus" OnItemCommand="Results_ItemCommand" InsertItemPosition="LastItem" UpdateMethod="UpdateClientStatus" InsertMethod="InsertClientStatus">
<LayoutTemplate>
<table>
<tr>
<th runat="server">
<asp:LinkButton ID="SortByDescription" runat="server" ClientIDMode="Static" CommandName="Sort" CommandArgument="Description" Text="Description" />
</th>
<th>Active</th>
<th></th>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</table>
<agp:PagerControl runat="server" ID="PagerControl" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%#: Item.Description%>
</td>
<td>
<%#: Item.IsClientActive %>
</td>
<td>
<asp:LinkButton ID="Edit" runat="server" ClientIDMode="Static" CommandName="Edit" CommandArgument="<%#: Item.ID %>" Text="Edit" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
When I add my EditItemTemplate, I have a Checkbox and I am trying to bind the Checked property to the model...
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description%>" />
</td>
<td>
<asp:CheckBox ID="IsActive" runat="server" Checked="<%#: BindItem.IsClientActive %>" />
</td>
<td>
<asp:LinkButton ID="Update" runat="server" ClientIDMode="Static"
CommandName="Update" CommandArgument="<%#: Item.ID %>"
Text="Update" />
<asp:LinkButton ID="Cancel" runat="server" ClientIDMode="Static"
CommandName="Cancel" CommandArgument="<%#: Item.ID %>"
Text="Cancel" />
</td>
</tr>
</EditItemTemplate>
This is where the problem starts, running the page now shows a message of "CS0030: Cannot convert type 'string' to 'bool'", prompting with the line...
<td>
<asp:CheckBox ID="IsActive" runat="server" Checked="<%#: BindItem.IsClientActive %>" />
</td>
What have I missed? How do I bind the value of IsClientActive to the Checked property of the Checkbox control? It is worth noting that, within the model, the IsClientActive property is defined as a Boolean and not nullable.
My bad; Checked="<%#: BindItem.IsClientActive %>" should have been Checked="<%# BindItem.IsClientActive %>" (note the omission of the colon (:))
I have the following inside a formview, I want to be able to show the information and if anyone makes changes I should update this information by clicking on update.I am fairly new to asp.net development , how can I do it? I keep having exception, saying frm1 is expecting editmode.
Thanks in advance
<<asp:Formview D="FormView1" runat="server" DataSourceID="SqlDesc" ID="frm1">
<ItemTemplate>
<table>
<tr>
<td>
Description:<br />
</td>
<td style="">
<asp:TextBox ID="DescTbox" runat="server" Width="450px" TextMode="MultiLine" Text='<%# Bind("Description") %>' Enabled="True" Rows="4"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update"> </asp:Button>
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td>
Description:<br />
</td>
<td style="">
<asp:TextBox ID="DescTbox" runat="server" Width="450px" TextMode="MultiLine" Text='<%# Bind("Description") %>' Enabled="True" Rows="4"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update"> </asp:Button>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:Formview>
Your <Itemtemplate> should provide a read only view of the data if possible, with the Update only being available in the <EditItemTemplate>. You then need to put the row into edit mode to be able to edit a row by setting the EditRowIndex on the table.
I had to add DefaultMode as Edit and use EditItemTemplate instead of ItemTemplate
<asp:Formview ID="FormView1" runat="server" DefaultMode="Edit" >
<EditItemTemplate> ...
</EditItemTemplate> <asp:Formview>
Thanks
Currently i'm working on my final year project.
In a webpage which loads data in gridview, each row has on button which will popup a window and ask for error to write inside **textbox** and submit that error on server.
On the serverside i require two values, first one is the primary key of that row and the error that is written inside **textbox**. It is easy to get primary key value but i'm unable to get value in side the textbox.
I'm attaching the code of .aspx file:
<asp:GridView ID="gvPODetails" runat="server" DataSourceID="Inspection_SqlDataSource"
EnableModelValidation="True" AllowSorting="True" AutoGenerateColumns="False"
BackColor="#CCCCCC" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" ForeColor="Black" CellSpacing="2">
<Columns>
<asp:BoundField DataField="ProductCode" HeaderText="ProductCode" SortExpression="ProductCode" />
<asp:BoundField DataField="MaterialCode" HeaderText="MaterialCode" SortExpression="MaterialCode" />
<asp:TemplateField>
<ItemStyle BorderStyle="None" BorderColor="Transparent" BorderWidth="0px" />
<ItemTemplate>
<asp:LinkButton ID="lnkbtnOk" OnClick="Ok_Click" CommandArgument='<%# Eval("Identity")%>'
runat="server" Text="Ok"></asp:LinkButton>
<asp:LinkButton ID="lnkbtnReject" runat="server" Text="Reject"></asp:LinkButton>
<asp:Panel ID="popUp_Data" runat="server" CssClass="modelPopup" Style="display: none;">
<table style="padding: 10px 10px 10px 10px; width: 100%;">
<tr>
<td>
</td>
<td align="right">
<input id="close_popup" type="image" src="../Images/closebox.gif" />
</td>
</tr>
<tr valign="top">
<td align="right">
<asp:Label ID="lblError" Text="Error" runat="server" CssClass="fontStyle"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtError" runat="server" CssClass="ta"
TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td align="left">
<asp:Button ID="btnSubmit" runat="server" CssClass="sbmt" Text="Reject Item" OnClick="Reject_Click"
CommandArgument='<%# Eval("Identity")%>' />
</td>
</tr>
</table>
</asp:Panel>
<asp:ModalPopupExtender ID="popUp_Data_ModalPopupExtender" runat="server" DynamicServicePath=""
Enabled="True" BackgroundCssClass="modelBackground" PopupControlID="popUp_Data"
TargetControlID="lnkbtnReject" CancelControlID="close_popup">
</asp:ModalPopupExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label runat="server" ID="lblNoDataFound" Text="No Machine Found."></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
What i want is the data inside textbox with id txtError.
Since it is a repeating item, you cannot access it directly using its id. You can use FindControl instead as follows:
var textBoxAux = gridView.Rows[index].FindControl("txtError") as TextBox;
You need to find the index of the row in the event you are handling.
EDIT: finding the index...
Pass the DataItemIndex in the CommandArgument ( instead of the identity ) if applicable.
CommandArgument='<%# Container.DataItemIndex %>'
Ref: ASP.NET GridView RowIndex As CommandArgument