Label not becoming visible inside of repeater inside an updatepanel - asp.net

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.

Related

Control placed in runat=server table sets SQL parameter at NULL on Update

I have a page with FormView and a plain html table inside. The table serves for layout and contains child controls data-bind to SqlDataSource. My problem is that if I add runat=server attribute to the table declaration, all controls inside the table set my SQL parameters at NULL in DataSource_Updating event and thus the record gets updated with NULLs instead of actual values. If I don't add runat=server, everything works fine. Sample of my code:
<asp:FormView ID="SettingsFormView" runat="server" DataKeyNames="Id" DataSourceID="SettingsDataSource"
DefaultMode="Edit" Width="560px">
<EditItemTemplate>
<strong>Settings</strong>
<table runat="server" width="350px">
<tr>
<td width="160">
Time (sec)
<dx:ASPxSpinEdit ID="textTime" runat="server"
Height="21px" Number="0" Value='<%# Bind("Time") %>' Width="104px" />
<br />
</td>
<td>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
I want to be able to remove (set invisible) some rows in code behind, that's why I need to set runat=server. However I can't get anywhere with this because SQL record updates with NULLs. Please, advice what might be wrong in my code.
Try using this.
<!-- toggle through OnLoad (can use ID as well) -->
<asp:PlaceHolder runat="server" OnLoad="MakeVisibleOrNot">
<tr>
...
</tr>
</asp:PlaceHolder>
and in the code behind:
protected void MakeVisibleOrNot(object sender, EventArgs e)
{
((Control) sender).Visible = ConfigUtil.DisplaySummaryComment;
}

display a popup on a Repeater control with textbox and confirmation button

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.

ListView programmatically adding row after databind

What I would like to achieve:
I would like a user to be able in insert a row into the listview. BUT not into a database.
What I am stuck on:
Currently I am stuck on the OnItemCommand, i dont seem to be entering the method. Any help would be great Code below.
<LayoutTemplate>
<table>
<th>
</th>
<th class="grayHeader">
<asp:Label ID="lblHeader" runat="server" />
</th>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Button runat="server" ID="btnDeletePerson" Text="-" CommandName="deletePerson"/>
</td>
<td>
<asp:Label ID="lblPerson" runat="server" Text='<% #Eval("Person") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr>
<td>
<asp:Button ID="btnAddUser" runat="server" CommandName="Insert" ext="+" />
</td>
<td>
<asp:TextBox runat="server" ID="txtInsert"></asp:TextBox>
</td>
</tr>
</InsertItemTemplate>
Protected Sub ListGrantee_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
Select Case e.CommandName
Case "Insert"
Dim test As ListViewItem
test = New ListViewItem("test")
listGrantee.Items.Add(test)
Case ""
Case Else
End Select
End Sub
I would save you temporarily created Users in the ListView's Datasource flagged as temporary(add a new DataColumn). Afterwards you have to DataBind the Listview. Store the flag in an invisible control(label) so that it is saved in the ViewState on Postbacks.
Further to my comment - as I understand your problem, your OnItemCommand event handler is not getting triggered. Here's how I set up event handlers (using VS2008).
In the design view for the aspx file, I highlight the control that I am interested in. Then, in the "Properties" window, I click on the Event's button (the little lightning flash), and scroll down the list of events until I find the one I am interested in.
Double click in the column next to the event name - this will bring up the code behind page, with the shell of the event handler in place - including the all important "handles ...." clause.
Now enter your event handling code....
I had viewstate turned off. Which disabled the event mechanism, flipped view state back on problem solved.

DropDownList in ModalPopup/UpdatePanel intermittently does not fire SelectedIndexChanged

I've got a UserControl that's used inside of an UpdatePanel.
The UserControl is a fairly simple form that appears via a ModalPopupExtender (which is also part of the UserControl). There are four DropDownLists, as well as some other UI elements.
Three of the four DropDownLists have AutoPostBack="true", with SelectedIndexChanged events that fire on the server and cause some of the other DropDownLists to rebind.
Two of the three DDL's that AutoPostBack are working fine. One of them, which I only just added, is showing some strange behavior.
Let's say I bind five Items to it: 1, 2, 3, 4, 5. I set the SelectedIndex to 0, which makes 1 the selected item.
If I select 5 and then 1 and keep toggling back and forth, everything works fine. The postback occurs and SelectedIndexChanged fires. Every time.
If I ever select 2 or 4, the postback occurs but SelectedIndexChanged does not fire. Every time.
If I ever select 3, something bizarre happens and sometimes the value of the DDL reverts to 1. Even though breakpoints seem to show that it's not rebinding and no unexpected code is running. I know your first instinct will probably be that I'm wrong about the rebinding code not running, but I have literally been staring at the debugger for hours trying to find my mistake. Lots of breakpoints. I don't get it -- this really isn't that complicated.
But obviously I am missing something.
I've put about four hours into this so far and I think I'm just grinding at this point. I could use another perspective.
HTML (and by the way, DropProtocolCycleID is the problem control):
<asp:Panel ID="PanelPopupAssign" runat="server" Style="display:none; cursor: move; width:325px; background-color:Transparent;">
<BlueUI:Panel runat="server" ID="PanelPatientProtocol" Width="500px" HeaderText="Assign Protocol">
<table cellspacing="5">
<tr>
<td style="width:150px;"></td>
<td style="width:50px;"></td>
<td style="width:125px;"></td>
</tr>
<tr runat="server" id="TableRowCategory">
<td align="right">Category:</td>
<td colspan="2">
<asp:DropDownList runat="server" ID="DropProtocolCategories" CausesValidation="false" autopostback="true"/>
</td>
</tr>
<tr>
<td align="right">Protocol:</td>
<td colspan="2">
<asp:DropDownList ID="DropProtocolID" runat="server" Enabled="false" CausesValidation="false" autopostback="true"/>
<asp:Label ID="LabelProtocolName_SetDate" runat="server" />
</td>
</tr>
<tr>
<td colspan="3">
<table style="margin-left: 120px">
<tr>
<td align="right">Cycle:</td>
<td><asp:DropDownList ID="DropProtocolCycleID" runat="server" autopostback="true" /></td>
</tr>
<tr>
<td align="right">Day:</td>
<td>
<asp:DropDownList ID="DropProtocolCycleDayID" runat="server" Enabled="false" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="right">Start Date:</td>
<td colspan="2">
<table>
<tr>
<td>
<asp:Textbox ID="TextProtocolStartDate" runat="server" Width="65px"
BackColor="Transparent" BorderStyle="None" ReadOnly="True" Font-Size="11px"
ForeColor="#1C4071" Font-Names="Verdana" ValidationGroup="AssignProtocol" />
</td>
<td>
<img id="ImageProtocolStartDate"
alt="Calendar"
onclick="CalProtocolStartDate.show();"
class="calendar_button"
src="../../Images/Icons/btn_calendar.gif"
width="25"
height="22" />
<asp:RequiredFieldValidator ID="ValRequiredProtocolStartDate" runat="server" display="Dynamic"
ControlToValidate="TextProtocolStartDate" ErrorMessage="Protocol Start Date is required!"
InitialValue="(None)"
Enabled="false" ValidationGroup="AssignProtocol">*</asp:RequiredFieldValidator>
</td>
</tr>
</table>
</td>
</tr>
</table>
<ComponentArt:Calendar runat="server"
id="CalProtocolStartDate"
AllowMonthSelection="false"
AllowMultipleSelection="false"
AllowWeekSelection="false"
CalendarCssClass="calendar"
TitleCssClass="title"
ControlType="Calendar"
DayCssClass="day"
DayHeaderCssClass="dayheader"
DayHoverCssClass="dayhover"
DayNameFormat="FirstTwoLetters"
ImagesBaseUrl="~/Images/Calendar/"
MonthCssClass="month"
NextImageUrl="cal_nextMonth.gif"
NextPrevCssClass="nextprev"
OtherMonthDayCssClass="othermonthday"
PopUp="Custom"
PopUpExpandControlId="ImageProtocolStartDate"
PrevImageUrl="cal_prevMonth.gif"
SelectedDate=""
VisibleDate=""
SelectedDayCssClass="selectedday"
SelectMonthCssClass="selector"
SelectMonthText="¤"
SelectWeekCssClass="selector"
SelectWeekText="»"
SwapDuration="300"
SwapSlide="Linear"
AutoPostBackOnSelectionChanged="False"
PopUpCollapseDuration="0"
ClientSideOnSelectionChanged="onCalProtocolStartDateChange">
<ClientEvents>
<Load EventHandler="Calendar1_onLoad" />
</ClientEvents>
</ComponentArt:Calendar>
<br />
<div style="text-align:center;">
<asp:Button ID="ButtonSaveProtocol" runat="server" Text="Save" ValidationGroup="AssignProtocol" Enabled="false" />
<asp:Button ID="ButtonCancel" runat="server" Text="Cancel" CausesValidation="false" />
</div>
<br />
</BlueUI:Panel>
</asp:Panel>
<ajaxToolkit:ModalPopupExtender id="ModalPopupExtenderAssignProtocol" runat="server"
popupcontrolid="PanelPopupAssign" popupdraghandlecontrolid="PanelPopupAssign" CancelControlID="ButtonCancel"
targetcontrolid="ButtonAssignProtocol" BackgroundCssClass="modalBackground" RepositionMode="RepositionOnWindowResizeAndScroll" >
</ajaxToolkit:ModalPopupExtender>
Relevant codebehind:
Private Sub DropProtocolCycleID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropProtocolCycleID.SelectedIndexChanged
Me.Show()
Me.SetupDropProtocolCycleDayID()
End Sub
Public Sub Show()
Me.ModalPopupExtenderAssignProtocol.Show()
End Sub
Here's the code where I bind DropProtocolCycleID, if you're interested. It fires in the SelectedIndexChanged event of DropProtocolID, which actually works reliably:
Private Sub SetupDropProtocolCycleID()
If Me.DropProtocolID.SelectedValue = Constants.NothingSelected Then
Me.DropProtocolCycleID.Enabled = False
Exit Sub
Else
Me.DropProtocolCycleID.Enabled = True
End If
Dim ProtocolID As Integer = Me.DropProtocolID.SelectedValue
Dim ProtocolCycles As DataTable = ProtocolManager.GenerateCycleTable(ProtocolID)
Me.DropProtocolCycleID.DataSource = ProtocolCycles
Me.DropProtocolCycleID.DataTextField = "ProtocolCycleNumber"
Me.DropProtocolCycleID.DataValueField = "ProtocolCycleID"
Me.DropProtocolCycleID.DataBind()
If DropProtocolCycleID.Items.Count > 0 Then
Me.DropProtocolCycleID.SelectedIndex = 0
End If
End Sub
ProtocolCycleNumber and ProtocolCycleID are just integers. No chance of anything in there that could interfere with the javascript.
This solution is ugly, but it works, and at this point I need to just get it working and move on.
In a nutshell, I added an invisible button and then made the DropDownList's onchange event click the button with JavaScript whenever it gets changed. This gets around whatever problem we're dealing with here.
I added this JS to the page:
function IndexChanged() {
document.getElementById("ctl00$MainContent$AssignProtocolControl$ButtonIndexChanged").click();
}
I changed the DropDownList to call that:
<asp:DropDownList ID="DropProtocolCycleID" runat="server" onchange="IndexChanged();" />
I added the invisible button:
<asp:Button id="ButtonIndexChanged" Text="Index Changed" style="display: none;" OnClick="DropProtocolCycleID_SelectedIndexChanged" runat="server" />
...And that solved the problem. Please let me know if you discover a better solution.
Oh, and as for the issue with the value sometimes being reverted to 1, it was because I needed to have duplicate values in my ListIems -- the text varied, but the values were sometimes the same.
Apparently when you do that, ViewState botches the job of restoring state and selects the first matched value it finds. So I just made my value a little more elaborate and it works fine now.
I had the same problem, I solved it by adding modalpop.show()
e.g.
AutoPostback=true in the design file
protected void ddlCars_SelectedIndexChanged(object sender, EventArgs e)
{
//Do all your work here
mpEditCars.Show();
}

Enable/Disable table with javascript for multple instances on a single asp age

I just posted one question and got it answered very quickly, thank you.
I have a new problem, being I have a asp label which gets text dynamically set on it during instanation and then I have a onmousedown function tied to it call a javascript function to enable a table area that sitting below that is display - none by default. It all works fine until I put two of these user controls on a page. They both have the specificed label text correct but the javascript enable seems to set the display style attribute of the first usercontrol's table to block instead of the one that is sitting below the label that was clicked. I am sure this is because the script is running on the client side (which I really would like to keep) and all the user controls have the same id name (since they are all instaniations of the same user control) so the javascript to set the style display attribute just gets the first table. I can't seem to think of a good way to either dynamically name the table on instationation so i can specify this "uniquie" id name to the javascript or any other way to do this work.
the user control asp code is below:
function enableDivArea(objName) {
document.getElementById(objName).style.display = "block";
}
function disableDivArea(objName) {
document.getElementById(objName).style.display = "none";
document.forms[0].submit();
}<asp:Label style="cursor:pointer;color:#EA9156;font-family:Arial,Helvetica,sans-serif;font-weight:bold;" ID="m_emailAddressLabel" onmousedown="enableDivArea('EmailFormDivArea');" runat="server" Text="someone#emailaddress.com"></asp:Label>
<table id="EmailFormDivArea" style="display:none; border-style: outset; border-width: thin">
<tr>
<td>To: <asp:Label ID="m_sendEmailToLabel" runat="server" Text=""></asp:Label></td>
<td align="right"><asp:Label onmousedown="disableDivArea('EmailFormDivArea');" id="m_closeLabel" runat="server" Text="close"></asp:Label></td>
</tr>
<tr>
<td><asp:Label ID="m_fromLabel" runat="server" Text="First Name:" Visible="True"></asp:Label></td>
<td><asp:TextBox ID="m_firstNameBox" runat="server" Visible="True"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="Label1" runat="server" Text="Last Name:" Visible="True"></asp:Label></td>
<td><asp:TextBox ID="m_lastNameBox" runat="server" Visible="True"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="Label2" runat="server" Text="E-mail:" Visible="True"></asp:Label></td>
<td><asp:TextBox ID="m_emailBox" runat="server" Visible="True"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="Label3" runat="server" Text="Phone number:" Visible="True"></asp:Label></td>
<td><asp:TextBox ID="m_phoneNumberBox" runat="server" Visible="True"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="Label4" runat="server" Text="Message:" Visible="True"></asp:Label></td>
<td><asp:TextBox ID="m_messageBox" runat="server" Visible="True" Rows="6" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center"><asp:Button ID="m_sendMessageButton" runat="server" Text="Send Message"
onclick="m_sendMessageButton_Click" /></td>
</tr>
<tr>
<td colspan="2" align="center"><asp:Label runat="server" ID="m_statusLabel" Text="" Visible="true"></asp:Label></td>
</tr>
</table>
and the code behind looks like this:
protected void Page_Load(object sender, EventArgs e)
{
m_emailAddressLabel.Text = this.Parameter;
m_sendEmailToLabel.Text = this.Parameter;
}
Have the table runat server, and let the user control implement INamingContainer.
Construct the ID for the table, and set it programmatically (HtmlTable) in an overridden CreateChildControls, to for instance string.Concat(ID, "table").
EDIT: You also need dynamic ID references in the javascript.
You could use some <asp:PlaceHolder>'s for this, and then set this from code-behind in CreateChildControls,
but maybe it is just easier to move these small scripts inline, and emit scripts from code-behind,
setting client attributes on the asp:Label
Just a quick idea, do the tables both have the same ID? so the script only picks the first it finds?
make the ID unique!
use a asp:table and then
onmousedown="enableDivArea('<%=tableName.ClientID%>');"
First of all you can set the CssClass property for the style.
for hidding:
document.getElementById(theIdOfYourTable).style.display = 'none';
enabled goes the same
oops, I'm sorry, forgot that you can't use <%%> in a server tag (and doing this out of my head). Better for you would be to call the script like this:
onmousedown="enableDivArea(this);"
In your script you don't have to do document.getElementById anymore, since you get the element as a param already now.
I will keep that mind on the last reponse by Henk.
I acutally "cheated" in a new way with the help of your guys information. I dynamically added an attribute to the label that I wanted the javascript to run from the onmousedown from the Page_Load event and formatted the string how I wanted. Here is the line, it works fantastically (at least what I was looking for).
m_emailAddressLabel.Attributes.Add("onmousedown", "enableDivArea('" + EmailFormDivArea.ClientID + "');");
Thanks for all your help.

Resources