I'm trying to find the values of TextBoxes which are rendered in a Repeater though a UserControl, i.e. the Repeater has a Placeholder for the UserControl, and inside the UserControl is where the TextBox markup actually exists. I've done this before with TextBoxes directly inside of a Repeater before, which was fairly straight forward, and I'm wondering why this apparently can't be accomplished the same way. Here is the Default page with the Repeater, which contains a Placeholder...
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<form class="formee">
<fieldset>
<legend>Faculty Information</legend>
<div class="grid-4-12">
<asp:Label ID="lblFirstName1" runat="server" Text="First Name"></asp:Label>
<asp:Label ID="lblFirstName2" runat="server" Text="" ></asp:Label>
<asp:Label ID ="lblSalary" runat="server" Text="" ClientIDMode="Static"></asp:Label>
</div>
<div class="grid-6-12">
<asp:Label ID="lblLastName1" runat="server" Text="Last Name"></asp:Label>
<asp:Label ID="lblLastName2" runat="server" Text=""></asp:Label>
</div>
</fieldset>
</form>
<div id="repeaterDiv">
<asp:Repeater ID="rptBudget" runat="server" ClientIDMode="Static">
<ItemTemplate>
<asp:PlaceHolder ID="phBudget" runat="server" EnableViewState="true" />
<br />
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnAddBudgetControl" runat="server" Text="Add"
CausesValidation="false" OnClick="AddBudgetControl" CssClass="addBudgetControl"/>
<asp:Button ID="btnDisplayEntries" runat="server" Text="Display Entries" CausesValidation="false" OnClick="DisplayEntries" />
</div>
<div>
<asp:TextBox ID="txtTotalPercent" runat="server" ClientIDMode="Static"></asp:TextBox>
<asp:TextBox ID="txtGrandTotal" runat="server" ClientIDMode="Static"></asp:TextBox>
<asp:Label ID="lblCtrls" runat="server" Text=""></asp:Label>
</div>
...and the UserControl which is inserted in the place of the Placeholder...
<fieldset>
<legend>Faculty Salary Form</legend>
<table cellspacing="10" id="values">
<tr>
<td>
<asp:Label ID="lblServiceType" runat="server" Text="Service"></asp:Label>
<asp:DropDownList runat="server" ID="ddlServiceType" CssClass="serviceType" />
</td>
<td>
<asp:Label ID="lblSpeedCode" runat="server" Text="Speed Code"></asp:Label>
<asp:DropDownList runat="server" ID="ddlSpeedCode" CssClass="speedType" />
</td>
<td>
<asp:Label ID="lblPercentage" runat="server" Text="Percentage"></asp:Label>
<asp:Textbox ID="txtPercentage" runat="server" CssClass="percentCommitment" ClientIDMode="Static" EnableViewState="true" />
</td>
<td>
<asp:Label ID="lblTotal" runat="server" Text="Total"></asp:Label>
<asp:TextBox ID="txtTotal" runat="server" CssClass="amountCommitment" ClientIDMode="Static" EnableViewState="true"/>
</td>
<td>
<asp:Button ID="btnRemove" runat="server" Text="Remove Item" OnClick="RemoveItem" ClientIDMode="Static" CssClass="btnRemove" />
</td>
</tr>
<tr>
</tr>
</table>
</fieldset>
...but when the following code runs for the Display button's OnClick, I always get a null value for any and all TextBoxes (and DropDowns) in the UserControl...
protected void DisplayEntries(object sender, EventArgs e)
{
foreach (RepeaterItem repeated in rptBudget.Items)
{
TextBox txtPercentage = (TextBox)repeated.FindControl("txtPercentage");
if (txtPercentage == null)
{
lblCtrls.Text += " null; ";
}
else
{
lblCtrls.Text += txtPercentage.Text + "; ";
}
}
}
What's the best way to access these values? Thanks.
txtPercentage Textbox is located inside Usercontrol, so use the following helper method to retrieve it.
Helper Method
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;
return root.Controls.Cast<Control>()
.Select(c => FindControlRecursive(c, id))
.FirstOrDefault(c => c != null);
}
Usage
foreach (RepeaterItem repeated in rptBudget.Items)
{
TextBox txtPercentage =
(TextBox)FindControlRecursive(repeated, "txtPercentage");
...
}
Try loading the placeholder first and then looking for the textboxes in the placeholder:
Something like this:
pseudo
var ph = repeated.FindControl("phBudget");
var txtBox = ph.FindControl("txtPercentage");
/pseudo
You need to first get the UserControl, then use FindControl on the UC itself.
Related
I have a product catalog page. When you click on the "Add Product" button, page with a cart shows up. This page has a table with an ItemTemplate inside it. Is there any way to get the value from the textBox located inside that ItemTemplate and change the value in the column Total cost in the table by clicking the button? The main problem is that I cannot access the textBox since it's in the . Thank you.
Catalog page:
Cart page:
CartView.aspx
MasterPageFile="~/Page/Store.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="bodyContent" runat="server">
<div id="content" style="margin-left: 7%;">
<style>
#import url("/css/tableCart.css");
#import url("/css/ButtonsCart.css");
</style>
<h2 style="padding: 14px; color:Highlight;">Ваша корзина</h2>
<h3 style="padding: 14px; color:Highlight;">Товары, которые вы добавили в корзину, представлены здесь</h3>
<table id="Table1" V class ="simple-little-table">
<thead>
<tr>
<th></th>
<th>Название</th>
<th>Цвет</th>
<th>Глубина</th>
<th>Ширина</th>
<th>Цена</th>
<th>Количество</th>
<th>Итого</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repeater1" ItemType="Line.Models.CartLine"
SelectMethod="GetCartLines" runat="server" EnableViewState="false">
<ItemTemplate>
<tr>
<td><asp:Image ID="Image1" runat="server" style="height:45px; " ImageUrl=<%# Item.Product.Img %> /></td>
<td> <%# Item.Product.NameProduct %> <%# Item.Product.TypeProducts %></td>
<td><%# Item.Product.Colors %></td>
<td>
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<asp:Button ID="cmdUpdate" OnClick="cmdUpdate_Click1" runat="server" Text="Update" CommandName="MyUpdate" CommandArgument = '<%# Container.ItemIndex %>'/>
</td>
<td><%# Item.Size.Depth%></td>
<td><%# Item.Product.Price%></td>
</td>
<td>
<td>
<asp:Label ID="Label2" runat="server" Text="<%# ((Item.Quantity *
Item.Product.Price))%>"></asp:Label>
</td>
<td>
<asp:Label ID="txtAmount" runat="server" Text=""></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
<tfoot>
<tr>
<td colspan="3">Итого:</td>
<td colspan="2" ><%= CartTotal.ToString("c") %></td>
</tr>
</tfoot>
</table>
</div>
</asp:Content>
CartView.aspx.cs
using Line.Helpers;
using Line.Models;
using Line.Models.Repository;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Line.Page
{
public partial class CartView : System.Web.UI.Page
{
protected void Page_Load(object sender, RepeaterCommandEventArgs e)
{
}
public IEnumerable<CartLine> GetCartLines()
{
return SessionHelper.GetCart(Session).Lines;
}
public decimal CartTotal
{
get
{
return SessionHelper.GetCart(Session).ComputeTotalValue();
}
}
public string CheckoutUrl
{
get
{
return RouteTable.Routes.GetVirtualPath(null, "checkout",
null).VirtualPath;
}
}
public string ReturnUrl
{
get
{
return SessionHelper.Get<string>(Session, SessionKey.RETURN_URL);
}
}
protected void cmdUpdate_Click1(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "MyUpdate")
{
RepeaterItem rRow = Repeater1.Items[Convert.ToInt32(e.CommandArgument)];
TextBox tQty = (TextBox)rRow.FindControl("txtQty");
Label tAmount = (Label)rRow.FindControl("txtAmount");
tAmount.Text = tQty.Text;
}
}
}
}
Ok, so you can set the index of the button, and pick this up in the Repeater "item command"
So, for your button, you can/want say this:
I have qty, price, and amount in the repeater. and button.
So, the markup can look like this:
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<br />
Price: <asp:TextBox ID="txtPrice" runat="server" Width="130px" />
<br />
Amount: <asp:TextBox ID="txtAmount" runat="server" Width="130px" />
<br />
<asp:Button ID="cmdUpdate" runat="server" Text="Update"
CommandName="MyUpdate"
CommandArgument = '<%# Container.ItemIndex %>'/>
So, you are now free to enter Qty, amount in any of the repeated items.
I have this:
Now, my repeater is going accross - and I think you should be using a listview since that better supports a grid + columnar layout - but it really don't mater (listview, gridview, repeater - they all work the same).
So, note how in our button we have both command Name, and command argument. In command argument I pass the row of the repeater.
So, the code looks like this:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// update button in repeater clicked
// update amount based on qty, and price
if (e.CommandName == "MyUpdate")
{
RepeaterItem rRow = Repeater1.Items(e.CommandArgument);
TextBox tQty = rRow.FindControl("txtQty");
TextBox tPrice = rRow.FindControl("txtPrice");
TextBox tAmount = rRow.FindControl("txtAmount");
tAmount.Text = tQty.Text * tPrice.Text;
}
}
So, just pass the "index" of the repeater row as per above. We used this expression as command argument:
<asp:Button ID="cmdUpdate" runat="server" Text="Update"
CommandName="MyUpdate"
CommandArgument = '<%# Container.ItemIndex %>'/>
So, once you have the Item Index (row index), then you can run code against the one row and change that row as per above.
Edit:
So the full markup I have is this:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div style="border-style:solid;color:black;width:250px;float:left">
<div style="padding:5px;text-align:right">
Hotel Name: <asp:TextBox ID="txtHotelName" runat="server" Text ='<%# Eval("HotelName") %>' Width="130px" />
<br />
First Name: <asp:TextBox ID="txtFirst" runat="server" Text ='<%# Eval("FirstName") %>' Width="130px" />
<br />
Last Name: <asp:TextBox ID="txtLast" runat="server" Text ='<%# Eval("LastName") %>' Width="130px" />
<br />
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<br />
Price: <asp:TextBox ID="txtPrice" runat="server" Width="130px" />
<br />
Amount: <asp:TextBox ID="txtAmount" runat="server" Width="130px" />
<br />
<asp:Button ID="cmdUpdate" runat="server" Text="Update" CommandName="MyUpdate" CommandArgument = '<%# Container.ItemIndex %>' />
<div>
City : <asp:DropDownList ID="cboCity" runat="server" DataTextField="City"
DataValueField="City" Width="110px">
</asp:DropDownList>
<div style="float:right;text-align:center;margin-left:4px;">
<asp:Button ID="cmdAddCity" runat="server" Text="+" OnClick="cmdAddCity_Click" Height="16px" Width="12px" Font-Size="XX-Small" cssclass="btnPad" OnClientClick="AddCity();return false"/>
</div>
</div>
Active: <asp:CheckBox ID="chkActive" runat="server" Checked = '<%# Eval("Active") %>'/>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
The code to load up this repeater is this:
protected void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack == false)
LoadGrid();
}
public void LoadGrid()
{
cmdSQL.Connection.Open();
strSQL = "SELECT ID, FirstName, LastName, HotelName, City, Active from tblHotels ORDER BY HotelName";
using (SqlCommand cmdSQL = new SqlCommand(strSQL, new SqlConnection(My.Settings.TEST3)))
{
cmdSQL.Connection.Open();
rstTable.Load(cmdSQL.ExecuteReader);
Repeater1.DataSource = rstTable;
Repeater1.DataBind();
}
}
so the above fills out the repeater - as noted, for a grid like layout, I would use a listview. Drag a list view into a web page.
use the wizards to connect to the database. Now blow out all the templates, only leave the itemtemplate.
Your code to load up the grid - same as above.
now, in place of the repeater as per previous screen shots?
You get this:
So your multiple lines that your building? They should be based on and around a listview. And each row can thus be grabbed, and addressed just as I did per above.
Do a google for listview examples - there is a like a billion examples.
Once you have this setup, then each row is a "thing" that repeats for you automatic based on the data from the table.
The markup for the list view - it again quite much follows the same ideas and concepts as a repeator. the listview for above looks like this:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID">
<ItemTemplate>
<tr style="">
<td><asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /></td>
<td><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' /></td>
<td><asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' /></td>
<td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
<td><asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Eval("Active") %>' Enabled="false" /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">ID</th>
<th runat="server">FirstName</th>
<th runat="server">LastName</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">Active</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
so let say i have a user control which called 7 times in an aspx file, i put field validator control in that user control, the problem is everytime i click some buttons which cause postback, field validator always validate controls against all instances of my user control, what i want is to validate against specific user control, i have tried this :
<asp:TextBox runat="server" ID="DateNew" CssClass="time" Width="185px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Date can not be empty" ControlToValidate="DateNew" ValidationGroup='<%= ClientID %>' Text="*"> </asp:RequiredFieldValidator>
<asp:Button runat="server" ID="NewSchedule" Text="Schedule" CssClass="bgrey"
OnClientClick="CheckKeyValues()" OnClick="NewSchedule_Click" ValidationGroup='<%= ClientId %>'/>
this is not working... any correction? thanx before. btw some example would be great.
update : aspx file with a user control called 7 times
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="divError" class="n fail" style="display:none; margin:15px; width:2330px">
<h5>
The following errors were found
</h5>
<asp:ValidationSummary ID="ValidationSummary" runat="server" />
</div>
<div class="warea clearfix" style="width: 2350px">
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress" TestType="Interview 1" />
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress1" TestType="Interview 2"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress2" TestType="Interview 3"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress3" TestType="English Test"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress4" TestType="Medical Examination"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress5" TestType="Internal Psycho Test"/>
</div>
<div style="float:left;">
<uc1:ucApplicationProgress runat="server" id="ucApplicationProgress6" TestType="External Psycho Test"/>
</div>
</div>
user control file :
<h3><asp:Label runat="server" ID="TypeNew"/></h3>
<table>
<tr>
<td>Date</td>
<td>
<asp:TextBox runat="server" ID="DateNew" CssClass="time" Width="185px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Date can not be empty" ControlToValidate="DateNew">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Time</td>
<td>
<asp:TextBox runat="server" ID="HourNew" Width="40px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Time can not be empty" ControlToValidate="HourNew" Text="*">
</asp:RequiredFieldValidator>:
<asp:TextBox runat="server" ID="MinuteNew" Width="40px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Minute can not be empty" ControlToValidate="MinuteNew" Text="*">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Location</td>
<td>
<asp:TextBox runat="server" ID="LocationNew" TextMode="MultiLine"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Location can not be empty" ControlToValidate="LocationNew" Text="*">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Interviewer</td>
<td>
<asp:TextBox runat="server" ID="InterviewerNew" autocomplete="off" onchange="MarkFlag(this.id)"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Interviewer can not be empty" ControlToValidate="InterviewerNew" Text="*">
</asp:RequiredFieldValidator>
<asp:HiddenField runat="server" ID="InterviewerKeys"/>
<asp:HiddenField runat="server" ID="InterviewerEmail"/>
</td>
</tr>
<tr>
<td> </td>
<td><asp:Button runat="server" ID="NewSchedule" Text="Schedule" CssClass="bgrey" OnClientClick="CheckKeyValues()" OnClick="NewSchedule_Click" /></td>
</tr>
</table>
C# :
public String ValidationGroup
{
get
{
return RequiredFieldValidator1.ValidationGroup;
}
set
{
RequiredFieldValidator1.ValidationGroup = value;
RequiredFieldValidator2.ValidationGroup = value;
RequiredFieldValidator3.ValidationGroup = value;
RequiredFieldValidator4.ValidationGroup = value;
RequiredFieldValidator5.ValidationGroup = value;
NewSchedule.ValidationGroup = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
ValidationGroup = this.ID;
}
Expose a property on your Web User Control named ValidationGroup:
public String ValidationGroup
{
get { return RequiredFieldValidator1.ValidationGroup; }
set {
RequiredFieldValidator1.ValidationGroup = value;
NewSchedule.ValidationGroup = value;
}
}
Then set it to a unique value for each instance you create on your web form.
Give the validation group value anything like "Name" as i have given.
And add the same validation group to both controls on which validation is to be fired and the control whose post back should fir validation.
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Field value is required" ControlToValidate="TextBox1"
ValidationGroup="Name"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="Name" />
<asp:Button ID="Button2" runat="server" Text="Button" />
As in this case TextBox1 validation fires only when Button1 is clicked not on Button2 click event.
It seems I have had a lot of questions today.
What i want to do is save the text for each image.
I'm getting the right text for each picture into the textboxes at the moment.
The code behind:
var car = GarageBLL.LoadCar(Convert.ToInt32(CarId),
Convert.ToInt32(_memberId)); ImageRepeater.DataSource = car.Images;
ImageRepeater.DataBind();
protected void FinalizeNewCar(object sender, EventArgs e) {
Response.Redirect("/amcargarasjen"); }
**Code here for saving each edit into the right ImageId.**
ASP:
<asp:Repeater runat="server" ID="ImageRepeater">
<ItemTemplate>
<table>
<tr>
<td>
<a class="deleteLink" href="#" rel="<%#Eval("ImageId")%>" title="">
<asp:Image runat="server" ImageUrl="/Content/Images/Garage/DeleteButton.png" /></a>
<asp:Image Width="60" Height="45" ID="ImgCar" ImageUrl='<%# String.Format("/garageimages/{0}/{1}.{2}", CarId, Eval("ImageId"), Eval("Extension")) %>' runat="server" />
<asp:TextBox runat="server" Text='<%# Eval("Description") %>' ID="txtText"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="Button1" runat="server" Text="Fullfør" OnClick="FinalizeNewCar" />
Anyone got any ideas on how to do that?
Modify your Repeater HTML markup like below. Note the addition of a Hidden field to keep the reference of the current image ID.
<asp:Repeater runat="server" ID="ImageRepeater">
<ItemTemplate>
<table>
<tr>
<td>
<a class="deleteLink" href="#" rel="<%#Eval("ImageId")%>" title="">
<asp:Image runat="server" ImageUrl="/Content/Images/Garage/DeleteButton.png" /></a>
<asp:Image Width="60" Height="45" ID="ImgCar" ImageUrl='<%# String.Format("/garageimages/{0}/{1}.{2}", CarId, Eval("ImageId"), Eval("Extension")) %>' runat="server" />
<asp:TextBox runat="server" Text='<%# Eval("Description") %>' ID="txtText"></asp:TextBox>
<asp:HiddenField runat="server" Value='<%# Eval("ImageId") %>' ID="txtImageId"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="Button1" runat="server" Text="Fullfør" OnClick="FinalizeNewCar" />
CODE FinalizeNewCar Event Handler
protected void FinalizeNewCar(object sender, EventArgs e)
{
foreach (RepeaterItem item in ImageRepeater.Items)
{
Int32 imageId = Convert.ToInt32(((HiddenField) item.FindControl("txtImageId")).Value);
string description = ((TextBox)item.FindControl("txtText")).Text;
//You will get the imageId description here.
//Write your code to update the datatbase.
}
}
I need a way for a piece of data to presist across postbacks. Is there a way besides, cookies, and sessions?
I've got a listbox, when clicked, the selected index changed event takes a session which contains user id and feeds it to a method that fetches user's name, etc. Then I use FindControl method to locate a button inside the form view. I set the button's text property with data just fetched such as first and last name.
It works on the initial load, but if I click on other records in the listbox second time the text property is not set.
I did not write this code - I'm trying to make chages.
Here is the list box:
<asp:ListBox ID="lbxUpcommingEvents" runat="server" DataSourceID="odsUpcommingEvents"
Rows="6" DataTextField="Title" DataValueField="id" AutoPostBack="true"
Width="206px" OnSelectedIndexChanged="lbxUpcommingEvents_OnSelectedIndexChanged" />
Listbox'x on selected index changed:
protected void lbxUpcommingEvents_OnSelectedIndexChanged(object sender, EventArgs e)
{
pnlEventsSignUp.Visible = true;
string _selectedItemValue = lbxUpcommingEvents.SelectedValue.ToString();
LoadName();
}
protected void LoadName()
{
MembershipUser user = Membership.GetUser();
DataSetTableAdapters.MemberInfoTableAdapter da = new DataSetTableAdapters.MemberInfoTableAdapter();
Guid _memberId = Guid.Empty;
_memberId = new Guid(Session["myId"].ToString());
DataSet.MemberInfoDataTable dt = da.GetMember(_memberId);
if (dt.Rows.Count == 1)
{
DataSet.MemberInfoRow mr = dt[0];
RolloverLink rlnkSignUp = (RolloverLink)(fvEventSignUp.FindControl("rlnkSignUp"));
rlnkSignUp.Text = "I, " + mr.firstname + " " + mr.lastname + " will be attending this event.";
}
}
Here is markup for the form view where I set the RolloverLinkButton's text property.
<asp:Panel runat="server" ID="pnlEventsSignUp" visible="false">
<div class="dashedline" ></div>
<asp:FormView ID="fvEventSignUp" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="id" Width="100%" >
<ItemTemplate>
<h2>
<asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" />
</h2>
<div class="itemdetails">
<br />
location:
<h3>
<asp:Label ID="locationLabel" runat="server" Text='<%# ShowLocationLink(Eval("locationname"),Eval("location")) %>' />
</h3>
<p>
<asp:Label Text='<%# Eval("starttime","{0:D}") %>' runat="server" ID="itemdateLabel" CssClass="GeneralText" />
<asp:Label Text='<%# ShowDuration(Eval("starttime"),Eval("endtime")) %>' runat="server" ID="Label1" CssClass="GeneralText" />
</p>
</div>
<div class="downloadevent">
<a href="#">
<img src="images/icon_download_event.gif" alt="Download this event to your personal calendar"
width="15" height="26" /></a><a href='<%# "events_download.ashx?EventID=" + Convert.ToString(Eval("id")) %>'>Add
this event to your personal calendar</a>
</div>
<Club:ImageThumbnail ID="thumb1" runat="server" ImageSize="Large" PhotoID='<%# Eval("photo") %>' />
<p>
<asp:Label Text='<%# Eval("description") %>' runat="server" ID="descriptionLabel" />
</p>
<div class="dashedline" ></div>
<asp:Panel ID="panel1" runat="server" CssClass="actionbuttons" Visible='<%#User.IsInRole("Administrators") %>'>
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>
<asp:Label runat="server" ID="Label2" Text="Action<br />Required" Width="80px" />
</td>
<td>
<img src="images/RedArrow.jpg" alt="Red Arrow Right" />
</td>
<td>
<asp:CheckBox runat="server" ID="cbxCert" Height="30px" Text="Check to Confirm Attendance" /><br />
<asp:CustomValidator runat="server" ID="rfvConfirm"
ErrorMessage="You must check the box to continue" Font-Bold="true"
ForeColor="#cc0000" ClientValidationFunction="ensureChecked" /> <br />
<Club:RolloverLink ID="rlnkSignUp" runat="server"
/>
</td>
</tr>
</table>
</asp:Panel>
</ItemTemplate>
</asp:FormView>
</asp:Panel>
You can use ViewState. Since you are dynamically looking up the button, setting a property is quicker and we can then let Asp.Net find the button and set its text via one of the two methods I explain below:
public string rlnkSignUpText
{
get { return Convert.ToString(ViewState["rlnkSignUpText"] ?? "default value"); }
set { ViewState["rlnkSignUpText"] = value; }
}
Replace your code that searches for the button and assigns the text with:
rlnkSignUpText = "I, " + mr.firstname + " " + mr.lastname + " will be attending this event.";
Then use one of these methods to set the text to the control:
1) Use the control's OnPreRender event:
To render it, add OnPreRender="rlnkSignUp_PreRender" to your xaml like this:
<Club:RolloverLink ID="rlnkSignUp" runat="server" OnPreRender="rlnkSignUp_PreRender" />
Then in your codebehind:
protected void rlnkSignUp_PreRender(object sender, EventArgs e)
{
RolloverLink rlnkSignUp = (RolloverLink)sender;
rlnkSignUp.Text = rlnkSignUpText;
}
2) Bind the property and control together:
Instead of using the control's PreRender you can bind to the property with:
<Club:RolloverLink ID="rlnkSignUp" runat="server" Text='<%# rlnkSignUpText %>' />
You then need to call DataBind in your Page_PreRender() like so:
void Page_PreRender(object sender, EventArgs e)
{
DataBind();
}
Have you enabled ViewState in the control.
Enable View State
Within a gridview, I have an "Email" button and a "Delete" button that opens up a Modal popup window using the modalpopupextender. The problem is that the Email window opens when the button is clicked, but the Delete window does not open when its button is clicked. I am using both modalpopupextenders in the same way.
<asp:ButtonField ButtonType="Button" CommandName="Email" Text="Email" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="Delete" />
<%-- Email modal--%>
<asp:Panel ID="pnlPopupEmail" runat="server" Width="500px" Style="display: none">
<asp:UpdatePanel ID="updPnlEmail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnShowEmail" runat="server" Style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="mdlPopupEmail" runat="server" TargetControlID="btnShowEmail"
PopupControlID="pnlPopupEmail" CancelControlID="btnCancel" BackgroundCssClass="modalBackground" />
<div class="EmailPopup">
<table>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="EmailTo" runat="server" Width="350"></asp:TextBox>
</td>
</tr>
<tr>
<td>
From:
</td>
<td>
<asp:TextBox ID="EmailFrom" runat="server" Width="350"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="Subject" runat="server" Width="350"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td> <asp:TextBox ID="EmailMessageBox" Width="350" Height="150" runat="server" TextMode="MultiLine" Wrap="true"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:LinkButton ID="SendBtn" runat="server" OnClick="SendBtn_Clicked" Text="Send" />
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false" />
</td>
<td>
<asp:Label ID="theID" runat="server" Visible="false"></asp:Label>
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<%-- -----end email modal--%>
<%-- Start Delete Confirmation Modal --%>
<asp:Panel ID="DelConfirmPanel" runat="server" Width="500px" Style="display: none">
<asp:UpdatePanel ID="DelConfirmUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID ="TESTLabel" runat="server" Text="Are you sure?" ></asp:Label>
<asp:Button ID="DelModalButton" runat="server" Style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="modalPopUpDelConfirm" runat="server" TargetControlID="DelModalButton"
PopupControlID="DelConfirmPanel" CancelControlID="DeleteCancel" BackgroundCssClass="modalBackground" />
<div class="DeletePopup">
<asp:LinkButton ID="DelConfirmedButton" runat="server" OnClick="DeleteConfirmation_Clicked"
Text="Delete" />
<asp:LinkButton ID="DeleteCancel" runat="server" Text="Cancel" CausesValidation="false" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<%-- End Delete Confirmation Modal --%>
</div>
Then in the codebehind I have a method that checks the command name and updates the updatepanel and shows the modalpopupextender.
protected void btn_Clicked(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Email")
{
// a bunch of stuff that I am leaving out but is just changing fields in the
//table withing the modal popup
updPnlEmail.Update();
mdlPopupEmail.show();
}
if (e.CommandName.Equals("Delete"))
{
int index = Convert.ToInt32(e.CommandArgument);
String id = gvReservations.DataKeys[index].Value.ToString(); // get id
try
{
DelConfirmUpdatePanel.Update();
modalPopUpDelConfirm.Show();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
Any ideas why one works and not the other? Is there a good way to debug this kind of error? Thanks a lot for any help!
Figured it out.
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="Delete" />
This is where the problem was. I didn't realize that "Delete" was a reserved command name. So when this button was clicked an exception was being thrown saying that it couldn't delete from the table. Thus, the modal popup was never being shown. I figured it out by opening the javascript console in the chrome tools.
So to fix it, I changed the command name to something else and now everything works as expected.