C# Repeater focusing the first element after DataSource is changed - c#

I have a Repeater with a certain DataSource (consisting of a list of images). The Repeater holds ImageButtons.
The aspx:
<asp:Panel ID="panSearch" runat="server" ScrollBars="Vertical" BorderColor="#333333" BorderStyle="Inset" Width="500" Height="200">
<asp:Repeater ID="Repeater" runat="server">
<ItemTemplate>
<asp:ImageButton OnClick="imgSearchResult_Click" BackColor="#333333" ID="imgSearchResult" height="32" width="32" runat="server" ImageUrl='<%# Eval("ImageUrl") %>'/>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
Additionally, I have a TextBox, which has a TextChanged-event in code-behind. I do a few things in there and at the end, my Repeater's DataSource will be overwritten with a new List of images (those images are put into the ImageButtons).
Repeater.DataSource = ImageList;
Repeater.DataBind();
My problem: Whenever my Repeater.DataSource is changed, it "clicks" the first ImageButton inside the Repeater. How do I prevent that from happening?
Full code:
My TextBox:
<asp:TextBox ID="textSearch" runat="server" Width="80" OnTextChanged="textSearch_TextChanged" ForeColor="Black" />
My TextChanged event:
protected void textSearch_TextChanged(object sender, EventArgs e)
{
string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images/ORAS"));
List<System.Web.UI.WebControls.Image> ImageList = new List<System.Web.UI.WebControls.Image>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
System.Web.UI.WebControls.Image myImage= new System.Web.UI.WebControls.Image();
myImage.ImageUrl = (String.Format("~/Images/ORAS/{0}", System.IO.Path.GetFileName(item)));
ImageList.Add(myImage);
}
Repeater.DataSource = ImageList;
Repeater.DataBind();
}
When I click on an ImageButton inside the Repeater (which is executed when the text in my TextBox is changed):
protected void imgSearchResult_Click(object sender, ImageClickEventArgs e)
{
var selectedImage = sender as ImageButton;
if (img1.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img1.ImageUrl = selectedImage.ImageUrl;
}
else if (img2.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img2.ImageUrl = selectedImage.ImageUrl;
}
else if (img3.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img3.ImageUrl = selectedImage.ImageUrl;
}
else if (img4.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img4.ImageUrl = selectedImage.ImageUrl;
}
else if (img5.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img5.ImageUrl = selectedImage.ImageUrl;
}
else if (img6.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img6.ImageUrl = selectedImage.ImageUrl;
}
else
{
ErrorMessage("Please remove one Image first!", true);
}
}
Pageload:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
img1.ImageUrl = "~/Images/ORAS/Empty/000.png";
img2.ImageUrl = "~/Images/ORAS/Empty/000.png";
img3.ImageUrl = "~/Images/ORAS/Empty/000.png";
img4.ImageUrl = "~/Images/ORAS/Empty/000.png";
img5.ImageUrl = "~/Images/ORAS/Empty/000.png";
img6.ImageUrl = "~/Images/ORAS/Empty/000.png";
LoadImages();
}
}
(LoadImages is almost 1:1 what's in my TextChanged function)

I really am not sure how (why) ASP.NET WebForms does it, but if you hit Enter and the form posts back, it will find the first control that implements IPostBackEventHandler and execute whatever event is bound to that. ImageButton implements it and so that's why it keeps firing the click event even though you didn't click on it. And, once again, only if you hit Enter.
I think that behaviour happens because the data posted back - __EVENTTARGET and __EVENTARGUMENT - are empty. Then ASP.NET goes bonkers.
You can solve it by putting a dummy button at the top of the page (or masterpage) and hide it using the style attribute. so:
<asp:Button ID="dummy" runat="server" style="display:none" />
Then in the init or load of your page (or masterpage) put
Form.DefaultButton = dummy.UniqueID;
That will force the button to capture the enter press instead of the arbitrary image button.

Related

enable an asp.net panel on dropdownlist onselectindexchanged

I want to enable a panel based on dropdownlist selected value.
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl.SelectedValue == "A")
{
lnk.Style.Add("Display", "block");
panel1.Visible=true;
panel1.Enabled=true;
}
}
The panel is not getting displayed. I have set autopastback property of dropdownlist to true.Can someone please help me.
should be without the quotes:
panel1.Enabled = true;
but if you wanted to show hide a panel:
<asp:Panel ID="panel1" runat="server" Visible="False" >
...
</asp:Panel>
then the right way would be
panel1.Visible = true;//false to hide

How can I tell which button in a repeater got pressed?

I have the following markup in an .aspx file:
<asp:Repeater ID="ListOfAssignments" runat="server" OnItemDataBound="ListOfAssignments_ItemDataBound">
<ItemTemplate>
<asp:Label ID="AssignmentID" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="PathToFile" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="RemoveAssignment" runat="server" Text="Remove" OnClick="RemoveAssignment_Click"/>
</ItemTemplate>
</asp:Repeater>
There are two labels and a button in a fairly standard repeater control. The repeater is bound to a database, and populates the labels with records from the database.
Here's my problem: I have a click event for each button in the repeater. The RemoveAssignment_Click method is called when the user clicks any of the buttons. In the click event, I want to know the text of the two labels associated with whatever button the user clicked.
What I mean is, in this method:
protected void RemoveAssignment_Click(object sender, EventArgs e)
{
//code goes here
}
I want to be able to know the text of the labels that are adjacent to the button that was clicked. How do I do that?
What you are looking for is the Button.OnCommand Method:
This allows you to create multiple Button controls on a Web page and
programmatically determine which Button control is clicked.
So inside ListOfAssignments_ItemDataBound you'd assign the CommandArgument to the button, where the CommandArgument is the ID of the article to be deleted:
protected void ListOfAssignments_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button delButton = e.Item.FindControl("RemoveAssignment") as Button;
delButton.CommandArgument = //set to the value of AssignmentID
//rest of your code
}
}
And now your button should say to use your new OnCommand:
<asp:Button ID="RemoveAssignment" OnCommand="RemoveAssignment" runat="server" Text="Remove" />
And then you create the method:
protected void RemoveAssignment(object sender, CommandEventArgs e)
{
int articleIDToDelete = 0;
if (Int32.TryParse((string)e.CommandArgument, out articleIDToDelete))
{
//delete the article with an ID = articleIDToDelete
}
}
You can add CommandName and CommandArgument attributes to the button tag and use them as a hint. And then in your even handler you can do e.CommandName == "xxx" or e.CommandArgument == "xxx". You can also use CommandArgument to pass the actual strings. You just need to bind the data just like you would do with a label, text, etc:
<asp:Button ID="RemoveAssignment" runat="server" CommandArgument='<%#Eval("Label1")+","+ Eval("Label2")%>' Text="Remove" OnClick="RemoveAssignment_Click"/>
Then in the event handler you can do something like:
string[] args = e.CommandArgument.ToString().Split(new char[] {','});
string label1 = args[0];
string label2 = args[1];
This should get you going.
Hm, I know c# and wp but you should try to set a bool and use this logic: if it is false, repeat will continue and if it is true, it'll exit the repeat. try this:
bool exit;
void Button_Click(eventargs stuffy here)
{
exit = true,
}
void Repeat()
{
if (exit == false)
{
Repeat();
//Your code here
}
}

TextChanged doesn't fire

I am trying to generate textboxes when the I press button add more so this is the code for onclick
protected void Add_TextBoxes(object sender, EventArgs e)
{
int index = int.Parse(ViewState["pickindex"].ToString());
TextBox MyTextBox = new TextBox();
MyTextBox.ID = "tbautogenerated"+index.ToString();
MyTextBox.Text = "tbautogenerated" + index.ToString();
MyTextBox.Width= 250;
MyTextBox.MaxLength = 128;
MyTextBox.Attributes.Add("runat", "server");
MyTextBox.CausesValidation = false;
MyTextBox.AutoPostBack = true;
MyTextBox.TextChanged += new EventHandler(MyTextBox_TextChanged);
picktexts.Controls.Add(MyTextBox);
}
void MyTextBox_TextChanged(object sender, EventArgs e)
{
TextBox MyTextBox = sender as TextBox;
}
but when I change in the textbox the textChanged doesn't work !!! what's wrong ?
HTML Code
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div id="picktexts" runat="server">
<asp:TextBox ID="txtAdress" runat="server" MaxLength="128" Width="250" />
<asp:RequiredFieldValidator ControlToValidate="txtAdress" Display="Dynamic" ID="rfvAddress" Text="* Required" runat="server" />
<asp:Button ID="bt_addtxtbox" runat="server" Text="Add more" OnClick="Add_TextBoxes" CausesValidation="false" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
I think the event handlers are getting lost between posts. the way ASP.NET works, every time you post a page back to itself, all objects are instantiated again, and their state is recovered from the ViewState. Normally a control that's declared in the aspx would reassociate itself with events by the declaration in its tag, which is not the case here.
So try associating the event handlers again during the page load. Like this:
void Page_Load (object sender, EventArgs e)
{
foreach (Control c in picktexts.Controls)
{
((TextBox)c).TextChanged += new EventHandler(MyTextBox_TextChanged);
}
}
And see if it works.

Find Control Inside ListView Control

I want to find "Label" control with ID = "Label" inside the "ListView" control. I was trying to do this with the following code:
((Label)this.ChatListView.FindControl("Label")).Text = "active";
But I am getting this exception: Object reference not set to an instance of an object .
What is wrong here ?
This is aspx code:
<asp:ListView ID="ChatListView" runat="server" DataSourceID="EntityDataSourceUserPosts">
<ItemTemplate>
<div class="post">
<div class="postHeader">
<h2><asp:Label ID="Label1" runat="server"
Text= '<%# Eval("Title") + " by " + this.GetUserFromPost((Guid?)Eval("AuthorUserID")) %>' ></asp:Label></h2>
<asp:Label ID="Label" runat="server" Text="" Visible="True"></asp:Label>
<div class="dateTimePost">
<%# Eval("PostDate")%>
</div>
</div>
<div class="postContent">
<%# Eval("PostComment") %>
</div>
</div>
</ItemTemplate>
</asp:ListView>
Listview is a databound control; so controls inside it will have different ids for different rows. You have to first detect the row, then grab the control. Best to grab such controls is inside an event like OnItemDataBound. There, you can do this to grab your control:
protected void myListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var yourLabel = e.Item.FindControl("Label1") as Label;
// ...
}
}
If you want to grab it in Page_Load, you will have to know specific row and retrieve the control as:
var theLabel = this.ChatListView.Items[<row_index>].FindControl("Label1") as Label;
This function will get Author Name from a database, you just need to call your method to get Author Name and then return it
protected string GetUserFromPost(Guid? x)
{
// call your function to get Author Name
return "User Name";
}
And to bind label in the list view you have to do it in list view ItemDataBound Event
protected void ChatListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label lbl = e.Item.FindControl("Label") as Label;
lbl.Text = "Active";
}
}
Here are the list view aspx code changes (just add onitemdatabound="ChatListView_ItemDataBound"):
asp:ListView
ID="ChatListView"
runat="server"
DataSourceID="EntityDataSourceUserPosts"
onitemdatabound="ChatListView_ItemDataBound"
It should be Label1 in the arguement:
((Label)this.ChatListView.FindControl("Label1")).Text = "active";
This should be in a databound event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx
Try it:
protected void ChatListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item is ListViewDataItem)
{
var yourLabel = e.Item.FindControl("Label1") as Label;
// ...
}
}
One simple solution to this problem, which avoids the FindControl code is to place OnInit on your label.
This would change your page code to this: <asp:Label ID="Label" runat="server" Text="" Visible="True" OnInit="Label_Init"></asp:Label>
And in your code behind you will now have a function like this:
protected void Label_Init(object sender, EventArgs e)
{
Label lblMyLabel = (Label)sender;
lblMyLabel.Text = "My Text";
}

Repeater and Custom Control - Dynamically adding to the collection and retaining values

It has been so long since I've used Web Forms I find myself not remembering most of the perks.
I have a user control that has a button, a repeater and the ItemTemplate property of the repeater is another user control.
<asp:Button runat="server" ID="btnAdd" CssClass="btn" Text="Add" OnClick="btnAdd_Click"/>
<br/>
<asp:Repeater runat="server" ID="rptrRequests">
<ItemTemplate>
<uc1:ucRequest ID="ucNewRequest" runat="server" />
</ItemTemplate>
</asp:Repeater>
The idea is that when the user clicks on the Add button a new instance of the ucRequest is added to the collection. The code behind is as follows:
public partial class ucRequests : UserControl
{
public List<ucRequest> requests
{
get
{
return (from RepeaterItem item in rptrRequests.Items
select (ucRequest) (item.Controls[1])
).ToList();
}
set
{
rptrRequests.DataSource = value;
rptrRequests.DataBind();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
requests = new List<ucRequest>();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
var reqs = requests;
reqs.Add(new ucRequest());
requests = reqs;
}
}
After much googling I now remember that I should be binding the Repeater in the OnInit method in order for the ViewState to put the captured data of the controls within the ucRequest control on them between post backs but when I try to do that I will always have a single instance of the control on the Repeater since its Items collection is always empty.
How could I manage to do this?
Thanks in advance.
You just need control ids in view state stead of entire control collection.
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="ucRequests.ascx.cs"
Inherits="RepeaterWebApplication.ucRequests" %>
<asp:Button runat="server" ID="btnAdd" CssClass="btn" Text="Add"
OnClick="btnAdd_Click" />
<br /><asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="ucRequest.ascx.cs"
Inherits="RepeaterWebApplication.ucRequest" %>
<asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
private List<int> _controlIds;
private List<int> ControlIds
{
get
{
if (_controlIds == null)
{
if (ViewState["ControlIds"] != null)
_controlIds = (List<int>) ViewState["ControlIds"];
else
_controlIds = new List<int>();
}
return _controlIds;
}
set { ViewState["ControlIds"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
foreach (int id in ControlIds)
{
Control ctrl = Page.LoadControl("ucRequest.ascx");
ctrl.ID = id.ToString();
PlaceHolder1.Controls.Add(ctrl);
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
var reqs = ControlIds;
int id = ControlIds.Count + 1;
reqs.Add(id);
ControlIds = reqs;
Control ctrl = Page.LoadControl("ucRequest.ascx");
ctrl.ID = id.ToString();
PlaceHolder1.Controls.Add(ctrl);
}
Try to get the ucRequests during the OnItemDatabound event, at that point you can edit the content of itemtemplate of the repeater. You can get there after the postback caused by the click on the add button. Here's a sample with a similar scenario

Categories