display a popup on a Repeater control with textbox and confirmation button - asp.net

So i have a .NET page in which i have a repeater and a "delete" button for every record in this repeater.
what i want to do is to display a popup each time this button is clicked,where the user could enter a text and confirm the deletion.
i already have my deleting method working and i'm having trouble with the displying popup part.
i've tried AjaxToolkit : popupExtender but can' t configure it correctly , it doesn't popup doesn't recognise button id .
<ajaxToolkit:ToolkitScriptManager
ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<div
<p style="width: 125px; margin-left: 520px">
</p>
<asp:Repeater ID="Repeater1" runat="server" EnableTheming="true" onitemcommand="Repeater1_ItemCommand" >
<HeaderTemplate>
<table class="datatable fullwidthpercent">
<tr>
<td>ID&nbsp</td>
<td>&nbsp Nom&nbsp</td>
<td>&nbsp Prénom&nbsp</td>
<td>&nbsp Email&nbsp</td>
<td>&nbsp Adresse&nbsp</td>
<td>&nbsp Ville&nbsp</td>
<td>&nbsp Tél&nbsp</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>&nbsp<%# Eval("id") %>&nbsp</td>
<td>&nbsp<%# Eval("nom") %>&nbsp</td>
<td>&nbsp<%# Eval("prenom") %>&nbsp</td>
<td>&nbsp<%# Eval("email") %>&nbsp</td>
<td>&nbsp<%# Eval("adresse") %>&nbsp</td>
<td>&nbsp<%# Eval("ville") %>&nbsp</td>
<td>&nbsp<%# Eval("tel") %>&nbsp</td>
<td><asp:Button ID="Btn1" runat="server" CommandArgument= ' <%# Eval("id") %> ' Text="Eliminer" OnClick = "buttonEliminer_click" ></asp:Button></td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
<ajaxToolkit:PopupControlExtender runat="server" TargetControlID="Btn1"></ajaxToolkit:PopupControlExtender>
and i get the following error :
system.argumentexception argument 'expression' is not a valid value Event validation is enabled using <pages enableEventValidation="true"/>
PS: repeater and database access is working fine.

Try creating a fake button below the ModalPopupExtender such as:
<asp:button runat="server" id="btnFake" style="visibility: collapse;" />
Then set the TargetControlID="btnFake" in the ModalPopupExtender.
In the code behind you can set when a person clicks the delete button, i.e. the buttonEliminer_click routine to:
ModalPopupExtenderID.Show()
Then with buttons on the modal popup to delete from the repeater. This would also mean that you have to gather the ID from the row of the repeater and set it to a HiddenField, for example, in the panel that will be popped up. Then when a person clicks "Submit" on your popup the HiddenField will have the ID of the row to delete.
EDIT: a few notes on your code as well, the popup extender you have added is outside of a </form> section, the popup should be inside this.

Related

Using TextBox TextChanged event to enable or disable a button control in asp.net

I am using an asp TextBox control with its TextChanged event and my goal is to capture text as a user enters it. If there are one or more characters entered, I would like a button control to be enabled without the user having to leave the TextBox control.
My source code for the TextBox on the aspx page is
<asp:TextBox ID="NewSpendingCategoryTextBox" MaxLength="12" runat="server"
AutoPostBack="True"
OnTextChanged="NewSpendingCategoryTextBox_TextChanged"
ViewStateMode="Enabled" >
</asp:TextBox>
and my source code on the code behind page is
Protected Sub NewSpendingCategoryTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles NewSpendingCategoryTextBox.TextChanged
Dim strSpendingCategoryTextBox As String = Nothing
strSpendingCategoryTextBox = NewSpendingCategoryTextBox.Text
If strSpendingCategoryTextBox.Length <= 0 Then
Me.NewSpendingCategoryInsertButton.Enabled = False
Else 'strSpendingCategoryTextBox.Length > 0
Me.NewSpendingCategoryInsertButton.Enabled = True
End If 'strSpendingCategoryTextBox.Length <= 0
End Sub
So it appears I have to use javascript to enable or disable the insert button. Can someone guide me on how to get an element within a table? The table sits in a Panel as well.
below is the aspx code...
<asp:Panel ID="AddSpendingCategoryPanel" Visible="false" runat="server">
<table class="AddNewTable">
<tbody>
<tr>
<td>
<asp:Label ID="lblSpend" runat="server"
Text="Spending Category:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="txtSpend" MaxLength="12"
runat="server"
AutoPostBack="True"
OnTextChanged="txtSpend_TextChanged"
OnKeyDown="return CheckSpendTextBoxValue()"
ViewStateMode="Enabled" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button CssClass="frmbtn" ID="btnInsertSpend"
runat="server" Text="Insert" />
</td>
<td>
<asp:Button CssClass="frmbtn" ID="btnCancelSpend"
runat="server" Text="Cancel"
CausesValidation="False" />
</td>
</tr>
</tbody>
</table>
</asp:Panel>
Run this code in the OnKeyPress event or consider JavaScript. The textbox does not fire the Text_Changed event til Tab or Enter are used.
Simplify the boolean check.
Me.NewSpendingCategoryInsertButton.Enabled = (NewSpendingCategoryTextBox.Text.Length <> 0)
I'm not sure exactly how you would do it. But the ASP.NET code is executed on the server that is hosting the web page.
I'd highly recommended doing it on JavaScript which can be run client side. Hopefully this article is of use to you.
How to check if a textbox is empty using javascript

how to set the focus to a control by default?

i have a login control and a create user control i my web page...i want the cursor to be in the user name text box of the login control when the page loads...how can i do that??
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
Bingo..!!! Youuuuu did it...<asp:LoginName ID="LoginName1" runat="server" />.
</LoggedInTemplate>
<AnonymousTemplate>
<asp:DropShadowExtender ID="DropShadowExtender1" runat="server"
TargetControlID="Panel1"
Rounded="true"
Opacity=".38">
</asp:DropShadowExtender>
<asp:Panel ID="Panel1" runat="server"
BackColor="Silver">
<asp:Login ID="Login1" runat="server"
DestinationPageUrl="~/ViewCart_aspx/ViewCart.aspx"
Height="152px"
Width="396px"
RememberMeSet="True"
OnLoggedIn="ContinueButton_Click" >
<LayoutTemplate>
<fieldset>
<table border="0"
cellpadding="1"
cellspacing="0"
style="border-collapse:collapse;">
<tr>
<td>
<table border="0" cellpadding="0" style="height:152px;width:396px;">
<tr>
<td align="center" colspan="2">
<h3>Log In</h3> </td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server" Width="150px" TabIndex="0"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
ControlToValidate="UserName" ErrorMessage="User Name is required."
ToolTip="User Name is required." ValidationGroup="ctl01$Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
as u can see the UserName textbox is inside the login control..so i cannot access its property..how do i find the control??
EDIT: as you mentioned in comments, you want to set your login button to be clicked as a default button. For this you need to set this button as a default button.
Unfortunatelly, you didn't format you code properly as I asked in a comment to your question. So I assume the login button is located in the same name container as the username text box and its name is btnLogin and you could set this control as a default control with HtmlForm.DefaultButton property, so:
You could use Page.SetFocus for this. It sets the browser focus to the specified control:
Page.SetFocus(txtName);
If you want to reach your UserName textbox, you could use just:
var login1 = LoginView1.FindControl("Login1") as Login;
if (login1 != null)
{
var txtUserName = login1.FindControl("UserName");
if (txtUserName != null)
{
Page.SetFocus(txtUserName);
}
var btnLogin = login1.FindControl("btnLogin");
if (btnLogin != null)
{
Page.Form.DefaultButton = btnLogin.UniqueID;
}
}
But note:
For the LoginView control, when being
added onto a page, at a certain time,
only one Template (anonymous or
loggedIn ) is applied on the Control
instance, so at that time, we can only
retrieve the reference of those
controls in the active template( can't
access those in the non-active
template).
Write the code in Page load event like follows
textbox1.focus();
where ever it may be, by using the ID of the control you have to access the control in the code behind.
The DefaultFocus property seems to be suitable in this case:
// inside page_load, LoginUser is the Login control
Page.Form.DefaultFocus = LoginUser.FindControl("Username").ClientID;
Related question: Set focus to textbox in ASP.NET Login control on page load

Placeholder inside an UpdatePanel

A little pseudo code to provide some background:
I have an ASPX with a placeholder and a button
When the button is clicked it adds a web user control (uc1) to my placeholder
The uc has a button
When clicked it adds a different user controls (uc2) to the placeholder
Stepping through the code, if I look at the placeholder.controls.count before and after the button-click in #4 the count increases by one, as you would expect.
The problem is that the uc2 added in #4 doesn't appear on the screen. So, I wrapped my placeholder in an UpdatePanel. I've never used one before. So, I could refresh the placeholder after the uc2 was added.
relevant ASPX code
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server></ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:TabContainer ID="tcNavigation" runat="server" ActiveTabIndex="0">
<ajaxToolkit:TabPanel ID="tpWebMerchandising" runat="server" HeaderText="WEB">
<ContentTemplate>
<table cellpadding="3" cellspacing="0" border="0">
<tr>
<td valign="top" class="label">Attributes</td>
<td>
<asp:UpdatePanel runat="server" id="UpdatePanel1" updatemode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="phAttributes" runat="server"></asp:PlaceHolder><br />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnAddAttribute" runat="server" Text="Add Attribute" /> | <asp:Button ID="btnCreateAttribute" runat="server" Text="Create Attribute" />
</td>
</tr>
</table>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
relevant ASCX code
For Each ctrl As Control In Parent.Page.Controls
For Each ctrl2 As Control In ctrl.Controls
For Each ctrl3 As Control In ctrl2.Controls
For Each ctrl4 As Control In ctrl3.Controls
For Each ctrl5 As Control In ctrl4.Controls
If InStr(ctrl5.GetType.ToString(), "UpdatePanel") > 0 Then 'UPDATE PANEL
up = ctrl5
End If
For Each ctrl6 As Control In ctrl5.Controls 'CONTENT TEMPLATE
For Each ctrl7 As Control In ctrl6.Controls 'PLACE HOLDER
If ctrl7.GetType.ToString() = "System.Web.UI.WebControls.PlaceHolder" Then
phAttributes = ctrl7
End If
Next
Next
Next
Next
Next
Next
Next
Dim ctrlAttributes As New AttributeControl
ctrlAttributes.AttributeName = attrName
ctrlAttributes.AttributeValue = attrValue
ctrlAttributes.ID = "ctrlAttribute-" & attrName
phAttributes.Controls.Add(ctrlAttributes)
I tried this...
up.Update()
and this...
Me.Page.RegisterStartupScript("AutoPostBackScript", "__doPostBack('UpdatePanel1', '');")
The goal is to refresh the placeholder so the new ctrlAttributes is displayed on screen.
First of all, use Parent.FindControl instead of this mess of foreach-es.

Gridview with TextBox

i have an gridview control with a comment text, link button, and an
invisible (text box and a button to post to database.)
when i click on the link button i want to show the textbox.
can any one help me how to do this.
my gridview code:
<asp:GridView ID="grdComments" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="500px" cellpadding="3" cellspacing="3">
<tr/>
<td/>
<asp:Label runat="server" ID="lblLeftPad"></asp:Label>
<asp:Label runat="server" ID="lblComment" Text='<%# container.dataitem("CommentText") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lbtnReply" Text="Reply" runat="server" CommandName="CommentReply"></asp:LinkButton>
</td>
</tr>
<tr>
<td>
</asp:TextBox ID="txtReply" runat="server" Height="50px" Width="500px" Visible="false"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
If you are not using Javascript / AJAX then on the link button's click event set the textbox's visible value to true. The link button automatically sends a postback so this would work unless you have the link button set to not auto postback.
EDIT: To access link button
There are a few ways depending on how you setup your grid. If this is a Command Field or a button field that you are using then you can use the RowCommand and the e.CommandArgument to now which row you are on and then set the textbox to true. Below is a sample:
row = Integer.Parse(e.CommandArgument)
GridView1.Rows(row).Cells(1).Controls(1).Visible = True
The cell is set to the column you are wanting to work on and the controls # is set to the control you want to work with in the cell. There is more than one control created in a cell even if you only put a textbox. You can use the FindControl syntax to more reliably get at your control.
If you created a templated field with the link button then on the command argument for the link button set it's value to: =<%# CType(Container,GridViewRow).RowIndex %>
and the above code in the gridview's rowcommand will work.
OR you can set the link buttons click event to something like:
gridview1.rows(directcast(sender,LinkButton).CommandArgument).cells(1).Controls(1).visible = true
You can get to the link buttons click event in a templated field by editing the template from the GUI and double clicking on the link button.
I would recommend using the RowCommand option and using the FindControl Syntax to make your app more readable and easier to maintain.

Label not becoming visible inside of repeater inside an updatepanel

I have an UpdatePanel that contains a Repeater. in the ItemTemplate of the repeater there is a button and a label.
When the button gets pressed, it performs some functionality, and then sets the label to visible and disables the button.
However none of the UI changes are being made to the webpage.
Here is the code, which when stepping through in debugger appears to work fine:
protected void CommentRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "report")
{
(e.Item.FindControl("btnReportComment") as ImageButton).Enabled = false;
Label thanksLabel = (Label)e.Item.FindControl("lblReportedComment");
thanksLabel.Visible = true;
}
pnlCommentsUpdater.Update();
}
and the page's code (excluding code outside of the repeater)
<asp:UpdatePanel UpdateMode="Conditional" ID="pnlCommentsUpdater" runat="server">
<ContentTemplate>
<asp:LinkButton ID="lnkPhoto1Comments" runat="server" Text="0 Comments" OnClick="lnkPhoto1Comments_Click" CssClass="dark-gray regular bold"></asp:LinkButton>
<asp:Panel ID="pnlPhoto1Comments" runat="server" Visible="False">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="CommentRepeater_ItemCommand">
<ItemTemplate>
<br />
<hr width="100%" size="1" color="#CCCCCC" />
<table width="534" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="150" align="left" valign="top">
<span class="blue small bold"><%# Eval("PostedBy") %>,</span><br />
<span class="light-gray small bold"><%# Eval("DateCreated", "{0:g}") %></span>
</td>
<td width="20"></td>
<td width="252" align="left" valign="top">
<div STYLE="word-wrap:break-word;width:252px;left:0">
<span class="dark-gray small bold"><%# Eval("CommentText") %></span>
</div>
</td>
<td width="20"></td>
<td width="92" valign="bottom">
<asp:ImageButton ID="btnReportComment" runat="server" ImageUrl="../images/inappropriate_off.png" CssClass="domclickroll images/inappropriate_on.png images/inappropriate_on.png" AlternateText="Inappropriate" CommandName="report" CommandArgument='<%#Eval("CommentId") %>' /><br />
<asp:Label ID="lblReportedComment" runat="server" Visible="false" CssClass="Regular bold blue" Text="Thanks. We'll check it out!"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
As I said, the debugger shows it to be working fine, however it simply doesn ot show the label in the browser after clicking the button.
Anyone know what I'm doing wrong?
The error is: "Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled."
And I am calling
ScriptManager.GetCurrent(Page).RegisterPostBackControl(Repeater1);
in the page load, which I read in some sites is the solution, but it did not help.
Check out this blog post...
http://weblogs.asp.net/leftslipper/archive/2007/02/26/sys-webforms-pagerequestmanagerparsererrorexception-what-it-is-and-how-to-avoid-it.aspx
It contains a number of approaches to fixing this. With respect to your call...
ScriptManager.GetCurrent(Page).RegisterPostBackControl(Repeater1);
... I think you're supposed to pass the button to RegisterPostBackControl, and not the repeater. i.e pass it btnReportComment instead. From the reference above...
3.Call ScriptManager.RegisterPostBackControl()
and pass in the button in question.
This is the best solution for controls
that are added dynamically, such as
those inside a repeating template.
First step is to narrow down your problem. If you take out the UpdatePanel altogether, does it work OK?
Also, right off the bat I see that pnlPhoto1Comments.Visible is set to false... ? This is getting set correctly somewhere I suppose, otherwise you wouldn't even get the ItemCommand event. So probably not a problem.

Resources