in ASP.NET 4 Website
I need to fire a server side C# function when an ajax accordion header is clicked, passing the header data value to the function. The accordion is filled from a database. How do I create the event to call the C# function?
<ajaxToolkit:Accordion ID="acc1" runat="server" Width="300px"
HeaderCssClass="accHeader" ContentCssClass="accContent" CssClass="accMain"
FadeTransitions="true" SuppressHeaderPostbacks="false" TransitionDuration="250" RequireOpenedPane="false"
HeaderSelectedCssClass="accSelHeader" FramesPerSecond="40">
<HeaderTemplate><b><%#DataBinder.Eval(Container.DataItem, "CompanyName") %></b></HeaderTemplate>
<ContentTemplate>
<%#DataBinder.Eval(Container.DataItem, "CompanyName") %><br />
<%#DataBinder.Eval(Container.DataItem, "Street")%><br />
<%#DataBinder.Eval(Container.DataItem, "City")%><br />
<%#DataBinder.Eval(Container.DataItem, "StateOfFacility")%><br />
<%#DataBinder.Eval(Container.DataItem, "ZipCode")%><br />
<%#DataBinder.Eval(Container.DataItem, "Phone")%><br />
<%#DataBinder.Eval(Container.DataItem, "URL")%><br />
</ContentTemplate>
</ajaxToolkit:Accordion>
Use LinkButton in header and handle ItemCommand event of Accordion as below:
<HeaderTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("CompanyName") %>'
CommandName="Select" CommandArgument='<%# (int)Eval("Id") %>' />
</HeaderTemplate>
void acc1_ItemCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "Select")
{
var companyId = e.CommandArgument;
}
}
Draw your attention that you need to provide companies unique identifier to distinguish clicked company pane.
Related
I have a fairly simple FormView control I'm using with ASP.Net/C#. My issue is when I use the code to redirect after the update/edit, the FormView won't actually perform the update, but, it will Redirect. I won't post all of the FormView code because it does update when I comment out the Redirect code. I think what I am asking is how to change the code to update while using the Redirect Code? Again, the update works fine when I don't do the Redirect! Is it possible the redirect is happening before the update can take place?
Code for Redirect
<script runat="server">
protected void FormView1_ItemUpdating(Object sender, FormViewUpdateEventArgs e)
{
{
Response.Redirect("redirect_main.aspx");
}
}
</script>
FormView1
<asp:FormView ID="FormView1" runat="server" onitemupdating="FormView1_ItemUpdating" DataKeyNames="req_submitted_key" DataSourceID="SqlDataSource2" DefaultMode="Edit" >
<EditItemTemplate>
req_submitted_key:
<asp:Label ID="req_submitted_keyLabel1" runat="server" Text='<%# Eval("req_submitted_key") %>' />
<br />
Role_job_title:
<asp:TextBox ID="Role_job_titleTextBox" runat="server" Text='<%# Bind("Role_job_title") %>' />
<br />
requestname:
<asp:TextBox ID="requestnameTextBox" runat="server" Text='<%# Bind("requestname") %>' />
<br />
submitted_by_name:
<asp:TextBox ID="submitted_by_nameTextBox" runat="server" Text='<%# Bind("submitted_by_name") %>' />
<br />
Submitted_by_email:
<asp:TextBox ID="Submitted_by_emailTextBox" runat="server" Text='<%# Bind("Submitted_by_email") %>' />
<br />
submitted_date:
<asp:TextBox ID="submitted_dateTextBox" runat="server" Text='<%# Bind("submitted_date") %>' />
<br />
submitted_by_comment:
<asp:TextBox ID="submitted_by_commentTextBox" runat="server" Text='<%# Bind("submitted_by_comment") %>' />
<br />
approved_denied:
<asp:TextBox ID="approved_deniedTextBox" runat="server" Text='<%# Bind("approved_denied") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
Yes...it redirecting before the update. Take your redirect out of ItemUpdating and put it in ItemUpdated.
From: DetailsView.ItemUpdatint Event
DetailsView.ItemUpdating Event
Occurs when an Update button within a DetailsView control is clicked,
but before the update operation.
DetailsView.ItemUpdated Event
Occurs when an Update button within a DetailsView control is clicked,
but after the update operation.
DetailsView.ItemUpdated Event
Your code should look like:
protected void FormView1_ItemUpdated(Object sender, FormViewUpdateEventArgs e)
{
Response.Redirect("redirect_main.aspx");
}
The Asp.net website I am creating uses a Repeater to display a list of Strings concerning duplicates or missing entries from two databases. There is another list of strings created in parallel of SQL statements with one SQL statement corresponding to the same numbered string in the Repeater list. The number of strings in the lists depends on the databases chosen and can range from zero to 100+.
Question: Since the number of Repeater rows are unknown, I am trying to find some method to generate an unknown number of checkboxes/buttons (one for each row). When clicked, the checkbox/button will find the appropriate SQL statement in the other list for the row in a separate method. Does anyone have an idea as to how a variable number of checkboxes could be created?
This can be done with adding a CommandArgument to a button and assigning a Command to it, not a Click.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("myColumn") %>'></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" CommandArgument='<%# Eval("ID") %>' OnCommand="Button1_Command" Text="Button" />
<hr />
</ItemTemplate>
</asp:Repeater>
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
And then in code behind handle the OnCommand
protected void Button1_Command(object sender, CommandEventArgs e)
{
Label2.Text = e.CommandArgument.ToString();
}
If you want to read a CheckBox, you'll need slighty more code in code behind.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("field01") %>'></asp:Label>
<br />
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("itemid")) %>' />
<br />
<asp:Button ID="Button1" runat="server" CommandArgument='<%# Eval("itemid") %>' OnCommand="Button1_Command" Text="Button" />
<hr />
</ItemTemplate>
</asp:Repeater>
protected void Button1_Command(object sender, CommandEventArgs e)
{
Button btn = sender as Button;
RepeaterItem item = (RepeaterItem)btn.NamingContainer;
CheckBox cb = item.FindControl("CheckBox1") as CheckBox;
Label2.Text = "Item with ID " + e.CommandArgument + " has checkbox " + cb.Checked;
}
i am using a repeater control in asp.net and i want to use one more repeater control inside the existing repeater control.
Example:
<!-- start child repeater -->
<!-- end child repeater -->
use the following
<asp:Repeater ID="rp_resList" runat="server" >
<ItemTemplate>
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<asp:LinkButton ID="lnkResName" runat="server">Resource Name</asp:LinkButton>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<asp:Repeater ID="rp_tagsSkill" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %></h6>
</ItemTemplate>
</asp:Repeater>
<div id="controls">
<asp:ImageButton ID="imgBtnBookmark" runat="server" />
<asp:Button ID="btnLike" runat="server" Text="Button" />
<asp:Literal ID="litComments" runat="server" Text="Comment (7)"></asp:Literal>
<asp:Button ID="btnShare" runat="server" Text="Button" />
</div>
</ItemTemplate>
</asp:Repeat>
in the you define your other repeter
and for more detail follow the following link nested Repeter
In your itemdatabound find your child repeater and bind it.
I have a ListView inside a FormView that, for some strange reason, doesn't fire neither the ItemInsert nor the ItemCommand event.
I'm populating the ListView with a generic list. I bind the list to the ListView on the OnPreRenderComplete.
<asp:ListView runat="server" ID="lvReferences" DataKeyNames="idReference" OnItemInserting="ContractReferences_Inserting" OnItemDeleting="ContractReferences_Deleting" InsertItemPosition="LastItem" OnItemCommand="ContractReferences_Command" OnItemCreated="ContractReferences_ItemDataBound">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li class="obsItem">
<asp:TextBox ID="valRef" runat="server" Width="5px" Enabled="false" Text='<%#Bind("idProcessRecordRef") %>' />
<asp:TextBox id="txtRef" runat="server" Text='<%#Bind("description") %>' />
<asp:ImageButton ID="btDelete" runat="server" CommandName="Delete" ImageUrl="~/_layouts/web.commons/Images/eliminar.png" />
</li>
</ItemTemplate>
<InsertItemTemplate>
<li class="obsItem">
<asp:TextBox ID="valRef" runat="server" Width="5px" Enabled="false" />
<asp:TextBox id="txtRef" runat="server" />
<asp:ImageButton ID="btDetail" CausesValidation="false" OnClientClick="javascript:openPopup();return false;" runat="server" ImageUrl="~/_layouts/web.commons/Images/novo.png" />
<asp:ImageButton ID="btSaveDs" runat="server" CommmandName="Insert" CausesValidation="false" ImageUrl="~/_layouts/web.commons/Images/gravarObs.png" />
</li>
</InsertItemTemplate>
</asp:ListView>
My ItemDataBound method is:
protected void ContractReferences_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (!IsPostBack)
{
TextBox valRef = e.Item.FindControl("valRef") as TextBox;
TextBox txtRef = e.Item.FindControl("txtRef") as TextBox;
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "function openPopup(){ window.open('ContractPicker.aspx?c1=" + valRef.ClientID + "&c2=" + txtRef.ClientID + "');}", true);
}
}
So, basically, in the InsertItemTemplate I put a button that opens a LOV and populates my valRef and txtRef fields. I had to put a "return false" in order for the parent page to not postback (and I think the problem lies here...).
Then, when I click in the ImageButton with the CommandName="Insert", instead of firing the ItemCommand event, it enters once again in the ItemDataBound handler.
So, any ideas?
Thanks!
Well, after searching for a few days, I found out this is a known bug.
http://connect.microsoft.com/VisualStudio/feedback/details/328680/problem-accessing-controls-clientid-on-asp-net-listviews-itemcreated
So, it hasn't nothing to do with the popup, but with the ListView and the ItemDataBound itself. I put the ItemDataBound logic on the OnLoad (using a recursive FindControl), and it worked.
So, in the ItemDataBound, the ClientID of my controls is "wrong".
Stupid bug really...
First off, I am not dynamically creating any controls. This is the order I take to produce the error:
I have a listview on the page, when I click the edit link under the listview, I display a panel which is hidden by default. The panel has a few buttons on it along with some listboxes. When I click an item in the listbox or click one of the buttons, I get the following error:
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Again, I am not creating anything dynamically, I am just hiding the panel with the controls by default and then displaying them, so I am not sure why I am getting this error.
Here is some code:
PAGE LOAD
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["Albums"] = null;
Albums = AlbumCollection.GetAlbums(Common.GetUserName(),
ddlAlbumType.SelectedIndex);
lvwAlbums.DataSource = Albums;
lvwAlbums.DataBind();
}
}
When I click the edit link, this is the code that runs:
protected void lvwAlbums_RowEditing(object sender, ListViewEditEventArgs e)
{
this.AlbumId = int.Parse(
this.lvwAlbums.DataKeys[e.NewEditIndex].Values["AlbumId"].ToString());
this.AlbumName=
this.lvwAlbums.DataKeys[e.NewEditIndex].Values["AlbumName"].ToString();
Album album = new Album(this.AlbumId);
ViewState["AlbumId"] = this.AlbumId;
ViewState["AlbumName"] = this.AlbumName;
pnlAlbum.Visible = true; // This panel holds the controls
btnEditAlbum.Visible = true;
btnCancel.Visible = true;
EditAlbum(this.AlbumId);
this.lvwAlbums.EditIndex = e.NewEditIndex;
AlbumCollection.GetAlbums(Common.GetUserName(),ddlAlbumType.SelectedIndex);
}
If I click the cancel button, I get the error, but it also happens if click another button on the panel such as Add/Remove... Here is the code for the Cancel button:
pnlAlbum.Visible = false;
this.lvwAlbums.EditIndex = -1;
AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);
Here is the aspx/html for the ListView:
<asp:ListView ID="lvwAlbums"
runat="server"
GroupItemCount="5"
DataKeyNames="AlbumId,AlbumName"
OnItemEditing="lvwAlbums_RowEditing"
OnItemCommand="lvwAlbums_ItemCommand"
OnItemDeleting="lvwAlbums_RowDeleting"
OnSelectedIndexChanging="lvwAlbums_SelectedIndexChanging"
OnPagePropertiesChanging="lvwAlbums_PagePropertiesChanging">
<EditItemTemplate>
<td>
<div>
<asp:TextBox ID="txtAlbumName" runat="server"
Text='<%# Eval("AlbumName").ToString().Trim() %>' />
<asp:LinkButton ID="lnkView" runat="server" Text="View" CommandName="View"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
|
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
|
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
<br />
<span>Songs:
<%# Eval("total") %></span>
</div>
</td>
</EditItemTemplate>
<LayoutTemplate>
<asp:DataPager runat="server" ID="ItemDataPager" PageSize="20"
PagedControlID="lvwAlbums">
<Fields>
<asp:NumericPagerField ButtonType="Link" NumericButtonCssClass="pager" />
</Fields>
</asp:DataPager>
<table>
<tr>
<td>
<table>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder"></asp:PlaceHolder>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<asp:Literal ID="litAlbumName" runat="server"
Text='<%# Eval("AlbumName").ToString().Trim() %>' />
<br />
<asp:LinkButton ID="lnkView" runat="server" Text="View" CommandName="View"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
|
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
|
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
<br />
<span>Songs:
<%# Eval("total") %></span>
</td>
</ItemTemplate>
</asp:ListView>
Here is the markup for the Panel:
<asp:Panel ID="pnlAlbum" runat="server" Visible="false">
<asp:ListBox ID="lstAvailableSongs" runat="server" SelectionMode="Multiple">
</asp:ListBox>
<asp:Button ID="btnAddAll" runat="server" Text="Add All" OnClick="btnAddAll_Click" />
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnRemove" runat="server" Text="Remove" OnClick="btnRemove_Click" />
<asp:Button ID="btnRemoveAll" runat="server"
Text="Remove All"OnClick="btnRemoveAll_Click" />
<asp:ListBox ID="lstSelectedSongs" runat="server" SelectionMode="Multiple">
</asp:ListBox>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
<asp:Button ID="btnEditAlbum" runat="server"Text="Save"
ValidationGroup="CreateAlbum" OnClick="btnEditAlbum_Click" />
<asp:Button ID="btnSaveAs" runat="server" Text="Save As" ValidationGroup="CreateAlbum"
OnClick="btnSaveAs_Click" />
</asp:Panel>
Here is some extra info:
I put an update panel around one of the listboxes in the panel and when I clicked the edit link under a listview item, I received the following error:
Microsoft JScript runtime error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'ctl00_ctl00_InnerContent_MainContent_UpdatePanel4'. If it is being updated dynamically then it must be inside another UpdatePanel.
Putting an UpdatePanel around the whole asp.net panel resolved the issue above, but I still get the Failed to load viewstate error when clicking on Cancel or Add, etc...
First off, you probably need to rebind the ListView after setting the EditIndex. (honestly, I haven't used ListView at all, but this is how the other repeater controls work) What does "EditAlbum()" do?
Your code is a little odd... why do you have the same controls in your EditItemTemplate as in the ItemTemplate? Ie, the Edit button should only be in the ItemTemplate... Then EditItemTemplate should have a Save or Cancel button.
Bottom line... your control tree is different on LoadViewState than it is when SaveViewState was called. One thing you can do is override these methods and then put a breakpoint there to manually look at the Controls collection in the debugger. You will probably see that the controls inside the ListView are different. But try my first suggestion before you do this.
Question for you:
in your Page_Load you have
Albums = AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);
but in lvwAlbums_RowEditing(..) and in btnCancel_Click(...) you have
AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);
shouldn't these be (Albums = ...)
Albums = AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);