working of linkbutton in repeater - c#

I used a linkbutton in repeater which on click shows data in a label.Now i want that clicking again the same linkbutton hide that data,means same button for showing and hiding data.
there is a database with a table which contains ques-description,date,sub. by and ans.
On page load only question appears.
Now this is the design code:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "showanswers")
{
Control control;
control = e.Item.FindControl("date");
if(control!=null)
control.Visible = true;
control = e.Item.FindControl("subby");
if(control!=null)
control.Visible = true;
control = e.Item.FindControl("ans");
if(control!=null)
control.Visible = true;
}
And this is the html code i used:
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<table>
<b>Question<%#Container.ItemIndex + 1%>:</b><%#Eval("qstdsc") %><br />
<asp:linkbutton ID="Button1" Text="Ans." commandname="showanswers" runat ="server" /><br />
</table>
<table>
<asp:Label id="date" Text='<%# Eval("qstdat")%>' Visible="false" runat="server"/>
</table>
<table>
<asp:Label id="subby" runat="server" Text='<%# Eval("qstsubby")%>' Visible="false" />
</table>
<table>
<asp:Label id="ans" runat="server" Text='<%# Eval("qstans")%>' Visible="false" />
</table>
</ItemTemplate>
</asp:Repeater>
But i don't know how to hide data again clicking the same linkbutton.
Is it possible with a single button?

What hinders you to check if the label is visible and hide/show it accordingly?
protected void lnkBtnShowDataLabel_Click(Object sender, EventArgs e)
{
lblData.Visible = !lblData.Visible;
}

Related

Access textbox in itemtemplate

I have 2 textboxes and a button in a row, and a Repeater dynamically generates some rows at Page_Load function.
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><asp:Label CssClass="form-control" disabled="true" runat="server"><%# DataBinder.Eval(Container.DataItem, "sid") %></asp:Label></td>
<td><asp:TextBox CssClass="form-control" runat="server" ID="quiz1"></asp:TextBox></td>
<td><asp:TextBox CssClass="form-control" runat="server" ID="quiz2"></asp:TextBox></td>
<td><asp:Button ID="add" CommandName="add" runat="server" OnClick="addQuiz" Text="Add" CssClass="btn btn-success btn-sm form-control"/></td>
</tr>
</ItemTemplate>
</asp:Repeater>
How do I access the textboxes in the Repeater upon clicking the button in the row?
You use FindControl on the RepeaterItem which is the NamingContainer of the TextBoxes. You get it by casting the sender to Button (or Control) and cast it's NamingContainer property accordingly:
protected void addQuiz(Object sender, EventArgs e)
{
var btn = (Button) sender;
var item = (RepeaterItem) btn.NamingContainer;
var quiz1 = (TextBox) item.FindControl("quiz1");
var quiz2 = (TextBox) item.FindControl("quiz2");
}

How to get LinkButton text in nested Repeaters

I have problem getting the LinkButton text in nested Repeaters
<div>
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" > <%# Container.DataItem %></asp:LinkButton>
</h6>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkBtnTags = (LinkButton)rp_tagsTopics.FindControl("lnkBtnTags");
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}
Or you can make use of the ItemCommand event by specifying the CommandName and CommandArgument parameters of the LinkButton
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" CommandName="Redirect" CommandArgument='<%# Container.DataItem %>' > <%# Container.DataItem %></asp:LinkButton>
And in the handler use the parameters:
protected void rp_tagsTopics_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if( e.CommandName == "Redirect" )
{
Response.Redirect("~/WebsofWonder.aspx?tag=" + e.CommandArgument);
}
}
What you should do is use the sender argument in the LinkButton_Click handler to get access to the instance of LinkButton that was actually clicked, and has raised the Click event:
protected void LinkButton1_Click(object sender, EventArgs e)
{
// Use sender instead of trying to find the control within the Repeater
LinkButton lnkBtnTags = (LinkButton) sender;
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}

How to get the clicked LinkButton text in GridView to a label in Popup Panel

I am using a modal popup inside a GridView for a LinkButton.
what i need is to use the value of clicked LinkButton to appear on a label on Popup panel
<asp:GridView ID="GridView1" runat="server">
<Columns><asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Text='<%#Eval("empname") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField></Columns>
</asp:GridView>
<asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>
<asp:ModalPopupExtender ID="LinkButton1_ModalPopupExtender" runat="server" DynamicServicePath="" Enabled="True" TargetControlID="lnkDummy" PopupControlID="Panel1"> </asp:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" Height="164px" Width="284px" BackColor="#CCCCFF" ><br /><br />
<center><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br /><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
<asp:Button ID="Button1" runat="server" Text="Do"/></center>
</asp:Panel>
code
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton Lnk = (LinkButton)sender;
Label2.Text = Lnk.Text;
LinkButton1_ModalPopupExtender.Show();
}
LinkButton btndetails = (LinkButton)e.CommandSource;
you can now access the btndetails text where u wish to show.

How to use button in repeater control?

I am using asp.net 3.5 with c#.I want to invoke button click event inside repeater control.
<asp:Repeater ID="rptFriendsList"
runat="server"
onitemcommand="rptFriendsList_ItemCommand">
<ItemTemplate>
<asp:ImageButton ID="btnSave"
runat="server"
ImageUrl="~/Contents/Images/save_button.png"
CommandName="Schedule"
UseSubmitBehavior="False" />
</ItemTemplate>
</asp:Repeater>
but when i click to a button its giving an error
"Invalid postback or callback
argument. Event validation is enabled
using 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."
my purpose is to execute some code in button click which is placed inside the repeater.Please help me to solve this issue.Thanks in advance.
UseSubmitBehavior="False" this property you have used is not present with the image button have you over ridden imagebutton class and added this property.
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_OnItemCommand" DataSourceID="SqlDataSource1">
<ItemTemplate>
key1:
<asp:Label ID="key1Label" runat="server" Text='<%# Eval("key1") %>'></asp:Label><br />
key2:
<asp:Label ID="key2Label" runat="server" Text='<%# Eval("key2") %>'></asp:Label><br />
key3:
<asp:Label ID="key3Label" runat="server" Text='<%# Eval("key3") %>'></asp:Label><br />
<asp:TextBox ID="col1" runat="server" Text='<%# Eval("col1") %>'></asp:TextBox>
<asp:TextBox ID="col2" runat="server" Text='<%# Eval("col2") %>'></asp:TextBox>
<br />
<asp:linkbutton ID="Linkbutton1" commandname="Update" runat="server" text="Update" CommandArgument='<%# Eval("key1") +"|"+Eval("key2")+"|"+ Eval("key3") %>' />
<asp:linkbutton ID="Linkbutton2" commandname="Cancel" runat="server" text="Cancel" />
</ItemTemplate>
protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Update")
{
string col1 = ((TextBox)e.Item.FindControl("col1")).Text;
string col2 = ((TextBox)e.Item.FindControl("col2")).Text;
string allKeys = Convert.ToString(e.CommandArgument);
string[] arrKeys = new string[2];
char[] splitter = { '|' };
arrKeys = allKeys.Split(splitter);
SqlDataSource1.UpdateParameters["col1"].DefaultValue = col1;
SqlDataSource1.UpdateParameters["col2"].DefaultValue = col2;
SqlDataSource1.UpdateParameters["key1"].DefaultValue = arrKeys[0];
SqlDataSource1.UpdateParameters["key2"].DefaultValue = arrKeys[1];
SqlDataSource1.UpdateParameters["key3"].DefaultValue = arrKeys[2];
SqlDataSource1.Update();
Repeater1.DataBind();
}
}
This also happens when you have assigned the datasource and data bound your repeater in the OnLoad event rather than OnInit
I have used this bellow code and runs ok
use this bellow code in .aspx page
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>
Edit
</th>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td align="center">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Use this in .cs Make the event Repeater1_ItemCommand
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
// Do some stuff when the Edit button is clicked.
break;
// Other commands here.
default:
break;
}
}
You can't use button, because button create postback on click and also itemcommand of repeater called!
But, If you want to use asp:button instead of asp:linkbutton, you have to set UseSubmitBehavior property of button to false. its means, button dont make postback.
<asp:Button ID="btnAccept" runat="server" Text="Accept All" CssClass="vb-default vb-green vb-txt-light" CommandName="Accept" CommandArgument='<%# Eval("UserID") %>' UseSubmitBehavior="false" />
Set page EnableEventValidation="false".
if you're adding items server side, try to assign unique ID to each ImageButton

How to Edit data in nested Listview

I am using listview to display a list of items and a nested listview to show list of features to each item. Both parent and child listview need to able Insert,Edit and delete operation. It works fine for parent listview. But when I try to edit an child item, The edit button does not take it into Edit mode. Can you please suggest me what I am missing in my code?
<asp:ListView ID="lvParent" runat="server"
OnItemDataBound="lvParent_ItemDataBound"
onitemcanceling="lvParent_ItemCanceling" onitemcommand="lvParent_ItemCommand"
DataKeyNames="ItemID" onitemdeleting="lvParent_ItemDeleting"
oniteminserting="lvParent_ItemInserting" >
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
<div align="right">
<asp:Button ID="btnInsert" runat="server" Text="ADD Item" onclick="btnInsert_Click"/>
</div>
</LayoutTemplate>
<ItemTemplate>
<table runat="server" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td>
<div id="dvDetail">
<span >Description</span>
<asp:TextBox ID="txtDescription" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' TextMode="MultiLine" ></asp:TextBox>
</div>
<div id="dvFeature" >
<span>Feature List</span>
<asp:ListView ID="lvChild" runat="server"
InsertItemPosition="LastItem"
DataKeyNames="FeatureID" OnItemCommand="lvChild_ItemCommand"
OnItemCanceling="lvChild_ItemCanceling" OnItemDeleting="lvChild_ItemDeleting"
OnItemEditing="lvChild_ItemEditing" OnItemInserting="lvChild_ItemInserting" OnItemUpdating="lvChild_ItemUpdating"
DataSource='<%# DataBinder.Eval(Container.DataItem, "FeatureList") %>' >
<LayoutTemplate>
<ul >
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" ></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<span class="dvList"><%# DataBinder.Eval(Container.DataItem, "FeatureTitle")%></span>
<div class="dvButton" >
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="/Images/edit_16x16.gif" AlternateText= "Edit" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" />
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Delete" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" />
</div>
</li>
</ItemTemplate>
<EditItemTemplate>
<li>
<asp:TextBox ID="txtFeature" Text='<%# DataBinder.Eval(Container.DataItem, "FeatureTitle")%>' runat="server"></asp:TextBox>
<div class="dvButton">
<asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="/Images/ok_16x16.gif" AlternateText= "Update" CommandName="Update" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FeatureID") %>' Width="12" Height="12" />
<asp:ImageButton ID="btnCancel" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Cancel" CommandName="Cancel" Width="12" Height="12" CausesValidation="false" />
</div>
</li>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtFeature" runat="server"></asp:TextBox>
<div class="dvButton">
<asp:ImageButton ID="btnInsert" runat="server" ImageUrl="/Images/ok_16x16.gif" AlternateText= "Insert" CommandName="Insert" Width="12" Height="12" />
<asp:ImageButton ID="btnCancel" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Cancel" CommandName="Cancel" Width="12" Height="12" CausesValidation="false" />
</div>
</InsertItemTemplate>
</asp:ListView>
</div>
</td>
</tr>
<tr>
<td align="right">
<div id="dvButton" >
<asp:Button ID="btnSave" runat="server" Text="Save"
CommandName="Save"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' />
<asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="Cancel"
CommandName="Delete"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' />
</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
BindData();
}
}
private void BindData()
{
MyDataContext data = new MyDataContext();
var result = from itm in data.ItemLists
where itm.ItemID == iItemID
select new
{
itm.ItemID,
itm.Description,
FeatureList = itm.Features
};
lvParent.DataSource = result;
lvParent.DataBind();
}
protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView lvChild = sender as ListView;
lvChild.EditIndex = e.NewEditIndex;
lvChild.DataBind();
}
Edit:
protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView lvChild = sender as ListView;
lvChild.EditIndex = e.NewEditIndex;
lvChild.DataBind();
}
If I use "lvChild.DataBind()" in 'ItemEditing' event, the total list of child items goes away if I click 'edit'
protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView lvChild = sender as ListView;
lvChild.EditIndex = e.NewEditIndex;
}
if I get rid of 'lvChild.Databind' in ItemEditing event, it goes to Edit mode after clicking the 'edit' button twice . And though it shows textbox control of EditItemTemplate, it appears as a blank textbox (does not bind existing value to edit).
This is an interesting problem. Almost certainly a databinding issue. In order to enter edit mode you must do two things:
1) Set the EditIndex
2) Call DataBind()
In the case of nested repeaters though... when does Render get called? I suspect you will have to call DataBind() on the PARENT in order to render everything correctly. That being the case you may have to then set the EditIndex AGAIN, since you are rebinding the parent.
EDIT:
OK... I just tried this with a nested GridView and I did NOT have to DataBind() the parent to get the sub grid to enter edit mode. Now I have to downvote my own answer. :|
hope that will serve someone, somewhere.
Here is my code to get that to work:
1) I have a Listview wich hold a user control when editing. This User cotnrol has itself a listview inside
<asp:ListView runat=server ID=C_LV_MyObjects DataKeyNames="Id"
OnItemDataBound=DataBoundMyObjects OnItemEditing=ItemEditing
>
<LayoutTemplate>
<table runat=server id="itemPlaceholderContainer">
<tr>
<th>
Description
</th>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
text...
</td>
<td>
<asp:LinkButton runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</td>
<td>
<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</td>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td colspan=3>
<MyTag:MyUC ID=C_UC_MyUserControl runat=server
OnEditing=MyObjectEditing
/>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
No results found!
</EmptyDataTemplate>
</asp:ListView>
The code c# for this listview is as follows :
public int EditIndexComposition;
protected void ItemEditing(object sender, ListViewEditEventArgs e)
{
C_LV_MyObjects.EditIndex = e.NewEditIndex;
C_LV_MyObjects.DataBind();
}
protected void MyObjectEditing(object sender, EventArgs e)
{
ListViewEditEventArgs MyEvent = (ListViewEditEventArgs)e;
if (MyEvent != null)
EditIndexComposition= MyEvent.NewEditIndex;
C_LV_MyObjects.DataBind();
}
protected void DataBoundMyObjects(object sender, ListViewItemEventArgs e)
{
MyUC uc = (MyUC)e.Item.FindControl("C_UC_MyUserControl");
if (uc!=null)
{
uc.EditIndex = EditIndexComposition;
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
MyObject obj= (MyObject)dataItem.DataItem;
uc.DataSource=Myservice.GetDatasource(obj.Id);
uc.DataBind();
}
}
and the code of my Usercontrol is as follows :
<asp:PlaceHolder runat="server" ID="C_PH_ObjComposition">
<asp:ListView runat="server" ID="C_LV_AppaltatoreComposizione" DataSource="<% # DataSource %>"
DataKeyNames="Id" OnItemEditing="ItemEditing">
etc...
<ItemTemplate>
<tr>
<td>
<asp:LinkButton runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
Edit Mode
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
</asp:PlaceHolder>
with the following code c# :
public int EditIndex
{
get {return C_LV_ObjComposition.EditIndex;}
set { C_LV_ObjComposition.EditIndex=value;}
}
public event EventHandler Editing;
protected void ItemEditing(object sender, ListViewEditEventArgs e)
{
C_LV_ObjComposition.EditIndex = e.NewEditIndex;
if (Editing != null)
Editing(this, e);
}
When clicking on the edit button of the innerlistview, we store the index that was clicked and we trigger a function in the first container user control. This function is going to store in a global value the index cliked and triggers a databind of the outter list. Doing so we get the onitemdatabound, that will recreate our usercontrol with the proper values, we can then before the databinding of the usercontrol assign the index of the editing row.
That's all if you have any questions , please feel free to answer..
ciao!

Categories

Resources