access values of listview on code behind - asp.net

I am creating a listview and binding data from database.
<asp:ListView runat="server" ID="lstService" DataTextField="Name"
DataValueField="Id" AutoPostBack="True">
<LayoutTemplate>
<table runat="server" id="table1">
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td runat="server">
<asp:Label ID="NameLabel" runat="server"
Text='<%#Eval("Name") %>' Width="500px" Height="30px" />
<asp:Button CssClass="btn btn-default" ID="srvButton" runat="server"
Text="Add" OnClick="srvButton_Click" />
<div class="voffset3"></div>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
On code behind I am binding data on this field. I am fetching ID, Name column.
Now it is showing label and button with text "Add" on the button. But when I click on Add , I want to pass the value of "Id" associated with that list view item, how can I do that ?
protected void srvButton_Click(object sender, EventArgs e)
{
}

First add the CommandArgument to your Button with the value of the item Id:
<asp:Button CssClass="btn btn-default" ID="srvButton" runat="server" Text="Add" OnClick="srvButton_Click" CommandArgument='<%#Eval("Id")' />
Now you can access it in the srvButton_Click handler:
protected void srvButton_Click(object sender, EventArgs e)
{
Button myButton = (Button)sender;
int i = Convert.ToInt32(myButton.CommandArgument.ToString());
//----
}

You can change the OnClick to an OnCommand and send a CommandArgument along with it.
<asp:Button CssClass="btn btn-default" ID="srvButton" runat="server"
Text="Add" OnCommand="srvButton_Command" CommandArgument=<%# Eval("ID") %> />
And then in code behind
protected void srvButton_Command(object sender, CommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
}
Or you can use OnItemCommand event of the ListView along with the DataKeyNames property and use a button without an event bound to it.
<asp:ListView runat="server" ID="lstService" DataKeyNames="id"
OnItemCommand="lstService_ItemCommand">
<asp:Button CssClass="btn btn-default" ID="srvButton" runat="server" Text="Add" />
And then in code behind get the datakey from the ListView.
protected void lstService_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int id = Convert.ToInt32(lstService.DataKeys[e.Item.DataItemIndex].Values[0]);
}

Related

Textbox asp.net postback trigger twice on autopostback true

On textbox OnTextChanged event the postback cycle triggering twice. Breakpoints on both methods to understand the issue.
Here is my code sample
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="TextBox1" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true" />
</div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
Its code behind.
public static int cycle { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
cycle++;
Label1.Text = cycle.ToString();
}
Avoid using AUTOPOSTBACK, keep the OnTextChanged event trap
and add a button (hidden or not) to catch the return pression on the textbox
to produce the POSTBACK.
Here is an example
<asp:Panel runat="server" CssClass="col-md-2">
<asp:Panel runat="server" CssClass="form-group input-group" DefaultButton="BTN_Cerca">
<span class="input-group-btn">
<asp:Button runat="server" ID="BTN_Cerca" Text="Codice" CssClass="btn btn-secondary" ToolTip="Cerca in magazzino"/>
</span>
<asp:TextBox runat="server" ID="TXT_Search" CssClass="form-control" placeholder="Numero Articolo" OnTextChanged="TXT_Search_TextChanged" />
</asp:Panel>
</asp:Panel>

Cant access a button which is positioned in a listView in code behind?

Can someone tell me why i cant access a button which is positioned in a listView in code behind?
here is my list
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="LinkButton3" runat="server" Text='<%# Eval("Name") %>' OnClick="LinkButton3_Click"/>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
and i cant do this on page load
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(LinkButton3);
}
linkButton3 does not exist in current context. Any help will be appreciated
From the code you provided.
The Button is in the ItemTemplate. You can add an ItemDataBound event handler to the ListView. Then in that event you will be able to access the Button.
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="LinkButton3" runat="server" Text='<%# Eval("Name") %>' OnClick="LinkButton3_Click"/>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Then in code behind
protected void ListView1_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
//if this item is not being edited, find controls declared in ItemTemplate
if (ListView1.EditItem == null || (ListView1.EditItem != null && dataItem.DisplayIndex != ListView1.EditIndex))
{
Button btn = (Button)e.Item.FindControl("LinkButton3");
}
//if this item is being edited, find controls declared in the EditItemTemplate
if (ListView1.EditItem != null && dataItem.DisplayIndex == ListView1.EditIndex)
{
Button btn = (Button)ListView1.EditItem.FindControl("LinkButton3");
}
}

Can not get textbox value inside datalist

I am trying to get the value of textbox "Qty" inside a datalist. It does not work. What is wrong? I have both the CartItem label and the datelist inside the ajax updatepanel. Thanks for any help. Here is my code:
<asp:Label ID="CartItems" runat="server" Text="CartItem"></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" CellPadding="10"
DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
<ItemTemplate>
<asp:Label ID="product_id" runat="server"
Text='<%# Eval("product_id") %>' /><br/>
<asp:Label ID="product_name" runat="server"
Text='<%# Eval("product_name") %>' />
<br />
Qty
<br/>
<asp:TextBox ID="Qty" runat="server"></asp:TextBox>
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart"
onClick="ButtonAddToCart_Click"/>
<br />
</ItemTemplate>
</asp:DataList>
and here is the button click event. The CartItem has null value:
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
CartItem.Text = DataList1.FindControl("Qty").ToString();
}
TextBox Qty = (TextBox)DataList1.FindControl("Qty");
if(Qty != null)
{
CartItem.Text =Qty.Text;
}
You can use NamingContainer or Parent to access sibling controls
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
var button = sender as Button;
var textbox = button.NamingContainer.FindControl("Qty") as TextBox;
CartItem.Text = textbox.Text;
}
I prefer you to use item_command event of the datalist
do some think like this.
<asp:Label ID="CartItems" runat="server" Text="CartItem"></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" CellPadding="10" DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
<ItemTemplate>
<asp:Label ID="product_id" runat="server"
Text='<%# Eval("product_id") %>' /><br/>
<asp:Label ID="product_name" runat="server"
Text='<%# Eval("product_name") %>' />
<br />
Qty
<br/>
<asp:TextBox ID="Qty" runat="server"></asp:TextBox>
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart" CommandName="addtocart2" OnCommand="DataList1_ItemCommand"
/>
<br />
</ItemTemplate>
</asp:DataList>
Here is the item_command event that works.
public void DataList1_ItemCommand(object source, System.Web.UI.WebControls.CommandEventArgs e){
TextBox qtytxtbox = DataList1.FindControl("Qty") as TextBox;
}
You can do like this
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
Button ButtonAddToCart= (Button)sender;
DataListItem item = (DataListItem)ButtonAddToCart.NamingContainer;
var textbox = (TextBox)item.FindControl("Qty");
}
try this one
TextBox txtquantity = (TextBox)(e.Item.FindControl("Qty"));
simply use this......You will get the value in txtquantity

About Asp.Net DataList Commands And EventValidation

I have Datalist that is inside an updatepanel and it is in panel in modalpopupextender;
I can list items as I wanted. I am also putting 2 buttons for Delete and AddBelow. Here is the markup:
<asp:DataList ID="SpeedDialsDL" runat="server">
<ItemTemplate>
<table id="speedDialValueEditorTable" width="100%">
<tr>
<td width="275px">
<asp:Label ID="ValueLabel" runat="server" Text="Value"></asp:Label>
</td>
<td>
<asp:TextBox ID="ValueTextBox" runat="server" Text='<%# Eval("Value") %>' Width="340px"
Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Delete" runat="server" Text="Delete" CommandName="Delete"
CausesValidation="false" />
<asp:Button ID="AddNewButton" runat="server" Text="AddBelow" CommandName="AddBelow"
CausesValidation="false" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
And register evenst like following: (I have used both ItemCommand and DeleteCommand to see which works:)
protected void Page_Load(object sender, EventArgs e)
{
SpeedDialsDL.ItemCommand += SpeedDialsDL_ItemCommand;
SpeedDialsDL.DeleteCommand += SpeedDialsDL_ItemCommand;
}
void SpeedDialsDL_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "Delete":
this.DeleteFromList((string)e.CommandArgument);
break;
case "AddBelow":
break;
}
}
But when I click Delete Or AddBelow buttons I get following error:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
I have disabled eventvalidation of page but the event couldn't be caught...
What am I missing here?
A control registers its events during rendering and then validates the events during the post-back or callback handling. You're registering the events manually during Page_Load.
Try refactoring your code so you specify the event handler in the .aspx page, bind the item's Id to the button's CommandArgument, then get the bound item Id in the handler. I would also have separate event handlers for the different events, it's a little cleaner. Something like:
<asp:DataList ID="SpeedDialsDL" runat="server">
<ItemTemplate>
<table id="speedDialValueEditorTable" width="100%">
<tr>
<td width="275px">
<asp:Label ID="ValueLabel" runat="server" Text="Value"></asp:Label>
</td>
<td>
<asp:TextBox ID="ValueTextBox" runat="server" Text='<%# Eval("Value") %>' Width="340px" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Delete" runat="server" Text="Delete" OnClick="Delete" CausesValidation="false" CommandArgument='<%# Eval("Id") %>' />
<asp:Button ID="AddNewButton" runat="server" Text="AddBelow" OnClick="AddBelow" CausesValidation="false" CommandArgument='<%# Eval("Id") %>' />
</td>
</tr>
</table>
</ItemTemplate>
protected void Delete(object sender, EventArgs e)
{
Button b = (Button)sender as Button;
string boundItemId = b.CommandArgument;
//handle delete
}
protected void AddBelow(object sender, EventArgs e)
{
Button b = (Button)sender as Button;
string boundItemId = b.CommandArgument;
//handle add below
}

Accessing controls in the edititemtemplate of a listview

I am working with the listview control. By default I am showing the itemtemplate with an edit button. When the edit button is pressed the listview switches to the edititemtemplate. I need to populate one of the controls in the edititemtemplate based on the item being edited - I've tried accessing the control (via FindControl) in the ItemEditing event (and pretty much every other event as well), however the controls just don't seem to exist. I can access controls in the itemtemplate ok, but not the edititemtemplate.
Can anyone let me know how I can access a control held within the edititemtemplate of a listview, and from which event I should do so?
EDIT
I'm trying to access the control using this:
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListViewItem item = UnitsLV.Items[e.NewEditIndex];
ListBox tempLB = (ListBox)e.item.FindControl("ListBox3");
}
I've also tried in ItemDataBound and ItemCreated.
The listview declaration is:
<asp:Content ID="Content1" ContentPlaceHolderID="ColumnA" runat="server">
<asp:Panel ID="Panel1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="SummaryPnl" runat="server">
<asp:ListView ID="UnitsLV" runat="server" DataSourceID="DataLDS" DataKeyNames="u_uid"
InsertItemPosition="LastItem" OnItemInserting="UnitsLV_ItemInserting" OnItemDataBound="UnitsLV_ItemDataBound"
OnItemCreated="UnitsLV_ItemCreated" onitemediting="UnitsLV_ItemEditing">
<ItemTemplate>
<tr class="rowA">
<td>
<asp:Label runat="server" ID="UnitIDLbl" Text='<%# Eval("u_uid")%>'></asp:Label>
</td>
<td>
<%# Eval("u_Title")%>
</td>
<td>
<asp:LinkButton ID="EditBtn" runat="server" CommandName="Edit" CommandArgument='<%#Eval("u_uid") %>'
Text="Edit" />
</td>
<td>
<asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete" CommandArgument='<%#Eval("u_uid") %>'
Text="Delete" />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr class="rowB">
<td>
<br />
</td>
<td>
<br />
<asp:TextBox ID="TitleTB" runat="server" Text='<% #Bind("u_Title")%>'></asp:TextBox>
</td>
<td>
<br />
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
<asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
</td>
<td>
<asp:LinkButton ID="InsertBtn" runat="server" CommandName="Insert" Text="Insert" />
</td>
<td>
<asp:LinkButton ID="CancelBtn" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</InsertItemTemplate>
<EditItemTemplate>
<tr class="rowB">
<td>
<br />
<asp:Label runat="server" ID="UnitIDLbl" Text='<%# Bind("u_uid")%>'></asp:Label>
</td>
<td>
<br />
<asp:TextBox ID="TitleTB" runat="server" Text='<% #Bind("u_Title")%>'></asp:TextBox>
</td>
<td>
<br />
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
<asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
</td>
<td>
<asp:LinkButton ID="UpdateBtn" runat="server" CommandName="Update" Text="Update" />
</td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</EditItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server" width="100%">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="" width="100%">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</td>
</tr>
<tr id="Tr2" runat="server">
<td id="Td2" runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</asp:Content>
EDIT:
I've iterated over all the controls in the listview using code similar to below, and the control is still not visible. Does the ItemEditing event fire before the edit template is shown? If so, what event can I use to access the edit template controls?
foreach (Control child in control.Controls)
{
Control result = Find(child, id);
if (result != null)
{
return result;
}
}
**EDIT: **
I can access the controls in the edititemtemplate in the listview's ItemCreated event, however none they have no content (I'd assume the data hasn't been bound yet), so I can't get the key-value I need to do a lookup to get the data I need to populate the control.
I've figured out a way to do what I need to do, though I'm not terribly happy with it.
protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (UnitsLV.EditIndex > -1)
{
// Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
}
}
I don't know about previous editions but in Visual Studio 2010 you can easily access the edit item trhough the "ListView.EditItem" property like this:
private void myListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
myListView.EditIndex = e.NewEditIndex;
myListView.DataBind();
ListViewItem lvwItem = lvwLista.EditItem;
ListBox tempLB = (ListBox) lvwItem.FindControl("ListBox3");
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
ListBox myListBox = (ListBox)(((ListView)sender).EditItem.FindControl("ListBox1"));
}
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListViewItem item = UnitsLV.Items[e.NewEditIndex];
ListBox tempLB = (ListBox)e.item.FindControl("ListBox3");
}
I believe I found a typo in the above function. The second line should be
ListBox tempLB = (ListBox)item.FindControl("ListBox3");
What I did was replace "e.item" with "item"
I usually use the ItemDataBound Event... check the other options in ListItemType Enum
protected void UnitLV_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
ListBox myListBox = (ListBox) e.Item.FindControl("ListBox3");
}
}
this is similar to the accepted answer, but I think its author was really driving towards this:
protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem listViewDataItem = e.Item as ListViewDataItem;
if (UnitsLV.EditIndex == listViewDataItem.DataItemIndex)
{
// Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
}
}

Resources