Find Unique Identifier From Selected Item In ASP ListView - asp.net

I'm using a ListView and have two buttons that allows users to delete or edit a item from the list. Below is the code for the buttons:
<td>
<asp:ImageButton ID="ButtonEdit" runat="server" ImageUrl="~/Styles/Images/Edit.png" ToolTip="Edit" OnClick="ButtonEdit_Click" />
<asp:ImageButton ID="ButtonDelete" runat="server" ImageUrl="~/Styles/Images/Delete-Red-Cross.png" ToolTip="Delete" CommandName="Delete" />
</td>
When the user clicks the edit button I want to pass the unique identifier from the item in the list to the parameter string so I can retrieve it in the edit page. Does anyone have any ideas on how to retrieve the ID from the selected item?

You could use the ImageButton.CommandArgument property:
<td>
<asp:ImageButton ID="ButtonEdit" runat="server" ImageUrl="~/Styles/Images/Edit.png" ToolTip="Edit" OnClick="ButtonEdit_Click" CommandArgument='<%= ItemBeingOutput.ID %>'/>
<asp:ImageButton ID="ButtonDelete" runat="server" ImageUrl="~/Styles/Images/Delete-Red-Cross.png" ToolTip="Delete" CommandName="Delete" CommandArgument='<%= ItemBeingOutput.ID %>'/>
</td>

I figured out how to pass the Eval without the PostBackUrl property:
<asp:ImageButton ID="ButtonEdit" runat="server" ImageUrl="~/Styles/Images/Edit.png"
ToolTip="Edit" PostBackUrl='<%# string.Format("system/editsystem.aspx?SystemID={0}", Eval("SystemID")) %>' />

Related

asp.net - Listbox - InsertItemTemplate RequiredFieldValidator

I have a asp.net Listview and I have add a RequiredFieldValidator for edit operations in EditItemTemplate.
When I click "Edit" button and try to save the data with an empty CustomerNameTextBox then I get error "Please enter your name!"
This is OK
<%# Page Language="C#" UnobtrusiveValidationMode="None" AutoEventWireup="true" CodeBehind="frmMain.aspx.cs" Inherits="DB_mit_GridView.frmMain" %>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="CustomerIDLabel1" runat="server" Text='<%# Eval("CustomerID") %>' />
</td>
<td>
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
<%-- CustomerNameTextBox must not be empty when editing an existing record
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="CustomerNameTextBox" errormessage="Please enter your name!" />
</td>
But I want to check for empty CustomerNameTextBox not only when editing an existing record but also when I insert a new record.
So I add RequiredFieldValidator for Insert operations in InsertItemTemplate :
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="CustomerIDTextBox" runat="server" Text='<%# Bind("CustomerID") %>' />
</td>
<td>
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
<%-- CustomerNameTextBox must not be empty when insertig a new record
<asp:RequiredFieldValidator runat="server" id="reqName2" controltovalidate="CustomerNameTextBox" errormessage="Please enter your name!" />
</td
But as soon as I add RequiredFieldValidator to InsertItemTemplate I get the message "Please enter your name!" immediately after start of then page.
I have not clicked on the "Insert" button - the message just appears without any click.
So how can I make sure that certain textboxes are not empty when editing or insertig ? (I have no code behind)
You can add validation group for all textboxes and for Insert button
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.validationgroup(v=vs.110).aspx

Unable to get TextBox text in DataList

I have a textbox in a datalist I'm trying to access in edit mode:
<asp:DataList ID="dl1" OnEditCommand="dl1_EditCommand"
OnCancelCommand="dl1_CancelCommand" OnUpdateCommand="dl1_UpdateCommand"
runat="server">
...
<asp:TextBox ID="tbType" Width="600" runat="server" Text='<%# Eval("Type") %>' />
CodeBehind:
protected void dl1_UpdateCommand(object sender, DataListCommandEventArgs e)
{
TextBox tb = (TextBox)e.Item.FindControl("tbType");
}
My code executes, but the value of the textbox is always empty, even though I have a value in it! I don't get either my updated text or the default text - I get a null. It finds the textbox, I've even opened the inspector to view its text...
This hasn't happened before and I'm not sure what I'm doing wrong. I've never had a problem like this before...
Full disclosure - this is a datalist inside a usercontrol.
UPDATE
Showing EditItemTemplate tags as requested.
The value is "" - I get a reference to the textbox, but no value.
<EditItemTemplate>
<td><asp:LinkButton ID="lbEdit" runat="server" Text="Update" CommandName="update" CausesValidation="false" />
<br /><asp:LinkButton ID="lbCancel" runat="server" Text="Cancel" CommandName="cancel" CausesValidation="false" />
</td>
<td>
<asp:HiddenField ID="hfID" runat="server" Value='<%# Eval("Id") %>' />
<asp:TextBox ID="tbType" Width="600" runat="server" Text='<%# Eval("Type") %>' />
</td>
<td></td>
</EditItemTemplate>

How can I add and delete row on Telerik's RadListBox Control?

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

How can I allow editing in certain columns in a listview row in asp.net based on a value in another column of that row?

I am trying to design listview to only allow editing of certain columns in a row based on a value in another column of the same row. I have spent several hours attempting many things and researching on the web but to no avail. I have tried adding a condition in the EditTemplate and tried using various listviews events such as ItemEditing and ItemDataBound, but have not succeeding. Can anyone point me in the right direction on what I need to do and where?
I've done this in the past by just hiding a textbox control and showing a label control for items that i don't want to edit.
Here Item2 is only editable when Item1 = "1":
<EditItemTemplate>
<tr>
<td>
<asp:LinkButton runat="server" ID="lnkUpdate" Text="Update" CommandName="Update" />
<asp:LinkButton runat="server" ID="LinkButton1" Text="Cancel" CommandName="Cancel" />
</td>
<td>
<asp:TextBox runat="server" id="Item1_txt" Text='<%# Eval("Item1") %>' />
</td>
<td>
<asp:TextBox runat="server" id="Item2_txt" Text='<%# Eval("Item2") %>' Visible='<%# Eval("Item1") == "1" ? true : false %>' />
<asp:Label runat="server" id="Item2_lbl" Text='<%# Eval("Item2") %>' Visible='<%# Eval("Item1") != "1" ? true : false %>' />
</td>
</tr>
</EditItemTemplate>

Validating controls in ListView insert/edit templates

I added a RequiredFieldValidator to my InsertItemTemplate, and it seems to be working fine. The problem I am having, however, is that now I cannot do anything else in the ListView (like edit or delete items) UNLESS the required field has a value. Is there some way I can manually do the validation when the user clicks the 'Insert' button on the InsertItemTemplate, or some other little trick I can perform so the user doesn't have to first type in a value just to delete something else in the list?
Thanks
A_Nablsi,
Please provide the code for your solution to turn the Insert New validation controls off when in Edit/Update mode or turn Edit/Update validation controls off when both the Edit and Insert Rows are active at the same time. This code using your notional solution fails with a null reference to the updateButton.
LinkButton updateButton = LVTasks.EditItem.FindControl("UpdateButtonTask") as LinkButton;
updateButton.CausesValidation = false;
The solution that works is adding Validation Groups.
Include ValidationGroup="myVGEdit" with your Validator Control(s) in the EditItemTemplate and your Update button.
Include ValidationGroup="myVGInsert" with your Validator Control(s) in the InsertItemTemplate and your Insert button.
<asp:ListView ID="LVTasks" runat="server"
DataKeyNames="IDTask"
DataSourceID="LDS_LVTasks"
InsertItemPosition="FirstItem"
oniteminserting="LVTasks_ItemInserting"
onitemupdating="LVTasks_ItemUpdating"
onitemcommand="LVTasks_ItemCommand"
>
<EditItemTemplate>
<asp:TextBox ID="TaskUpdateTextBox" runat="server"
Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskUpdateTextBox"
ValidationGroup="myVGUpdate"
/>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"
ToolTip="Cancel - Abort - No Changes"><div class="Cancel"></div></asp:LinkButton>
<asp:LinkButton ID="UpdateButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Update"
CausesValidation="True"
ValidationGroup="myVGEdit"
ToolTip="Save Changes - Update"><div class="Update" ></div></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TaskInsertTextBox" runat="server" Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskInsertTextBox"
ValidationGroup="myVGInsert"
/>
<asp:LinkButton ID="CancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"><div class="Clear" ></div></asp:LinkButton>
<asp:LinkButton ID="InsertButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Insert"
CausesValidation="true"
ValidationGroup="myVGInsert"
><div class="Insert" ></div></asp:LinkButton>
</InsertItemTemplate>
Yes,
set the CausesValidation property to false on the controls you don't want them to trigger the validation.

Resources