Use same control ID in different Panels - c#

How can I get separate NamingContainers for different asp:Panel server controls?
In the following .aspx markup, I see that:
<asp:Panel runat="server" ID="Case1" CssClass="caseEvaluationContainer">
<asp:Label ID="NamingContainer1" Text=<%# Case1.NamingContainer.UniqueID %> Runat="server"></asp:Label>
<br />
<asp:Label ID="UniqueID1" Text="<%# Case1.UniqueID %>" Runat="server"></asp:Label>
</asp:Panel>
<asp:Panel runat="server" ID="Case4" CssClass="caseEvaluationContainer">
<asp:Label ID="NamingContainer4" Text=<%# Case1.NamingContainer.UniqueID %> runat="server"></asp:Label>
<br />
<asp:Label ID="UniqueID4" Text="<%# Case1.UniqueID %>" runat="server"></asp:Label>
</asp:Panel>
The UniqueIDs are shown to be different, but not the NamingContainers.
How can I get something like the following to work (I need controls with the same ID's on the page without using a separate databinding containers like asp:Repeater or asp:FormView.
<asp:Panel runat="server" ID="TestPanel1">
<asp:TextBox runat="server" ID="SameControlID"></asp:TextBox>
</asp:Panel>
<asp:Panel runat="server" ID="TestPanel2">
<asp:TextBox runat="server" ID="SameControlID"></asp:TextBox>
</asp:Panel>

You cannot use the same control ID on a Page. Even if these were just HTML DIVs with the same ID you would have a collision.
If you want to handle the code in one manner in the code behind make the contents of your panels into one user control. Then you can drop that one user control in each panel. In that way you can have one set of code to manage the control.
Code sample download.

I need controls with the same ID's on the page
You cannot. It violates the HTML rule.
ID must be document-wide unique.

One of the way that you can achieve by having a same class name of the control with different id like this
<input type="text" id="id1" class="same" />
<input type="text" id="id2" class="same" />

Related

ASP Grid inside of Bootstrap popover using Eval Tags

I have a ASP Gridview that has a column with a button that opens a popover. Inside the popover I want to populate an ASP Table or ASP Gridview to show hidden related data. I am able to open the popover and it displays hard coded data. But when I want to make the data result dynamic using the Eval() tag the syntax is not able to read the text="<%# Eval('Label1') %>" tags within the string within the object.
The purpose is to load the entire data set and the popover data on one page load. I am wanting to avoid reload if possible. It seems that it cannot handle the <% within the string, is there a way to handle for this?
Any advice is welcome. Thank you in advance.
Sample code:
<asp:Button ID="Button3"
runat="server"
class="btn btn-info btn-xs"
Text="+"
OnClientClick="return false;"
data-toggle="popover"
data-trigger="focus"
TabIndex="0"
data-placement="right"
title="Owner Change"
UseSubmitBehavior="true"
data-content='<asp:Gridview runat="server" ID="gdvTest">
<Columns>
<asp:TemplateField HeaderText="Label One">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label1') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Label Two">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label2') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
/>
Update: I also attempted to use ASP Table, but my class is not recognizing the ID im trying to use to dynamically populate the Label.
...data-content="<asp:Table><asp:TableRow><asp:TableHeaderCell text='TestHeader'><asp:Label runat='server' ID='lblTest' style='color:Red'></asp:TableHeaderCell></asp:TableRow></asp:Table></asp:Gridview>"
Your syntax is a bit off indeed. Remember that everything inside <%# %> should be a valid C#, so 'Label2' is an invalid literal. To fix that, swap quotes over:
text='<%# Eval("Label1") %>'
Update.
Missed the fact that GridView is itself surrounded by single quotes of data-content attribute. Even though this markup looks quite weird, there is a way to make it work, using double quotes everywhere and escaping appropriately:
text="<%# Eval("Label1") %>"

Adding Non-Standard Attribute to asp:LinkButton inside Repeater prevents the run of OnItemCommand

I am integrating a Foundation 5 Reveal Modal component with an <asp:LinkButton>. For the reveal modal to work it requires the addition of a non-standard HTML attribute data-reveal-id to the <asp:LinkButton>. If I do 'not' add this attribute in the .aspx below, then clicking the LinkButton will trigger rptNotice_ItemCommand. However, when data-reveal-id is added to the LinkButton as shown below rptNotice_ItemCommand does not run, and lbMessage is not updated. The lbMessage is required by the database upon submission of the modal form.
How do I get this to work?
Is there another way to keep track of the message ID?
In the .aspx file
<!-- Foundation 5 Reveal Modal -->
<div id="replyModal" class="reveal-modal" data-reveal>
<h2>Reply to Notice</h2>
<asp:Label ID="lbMessage" runat="server" />
<asp:TextBox ID='tbFollowQuestion' runat="server" TextMode="MultiLine" Width="100%" /><br />
<asp:Button ID="btnFollowCancel" runat="server" Text="Cancel" OnClick="followQuestionCancel_Clicked" />
<asp:Button ID="btnFollowQuestion" runat="server" Text="Submit" OnClick="followQuestionSave_Clicked" />
<a class="close-reveal-modal">×</a>
</div>
<asp:Repeater ID="rptNotice" runat="server" OnItemCommand="rptNotice_ItemCommand">
<ItemTemplate>
<div class="row padding-bottom-10 border-bottom-1 margin-bottom-10">
<!-- LinkButton -->
<div class="small-6 medium-2 xlarge-1 columns">
<asp:LinkButton data-reveal-id="replyModal" CommandName="Contact" CommandArgument='<%#Eval("MessageId")%>' ToolTip="Reply" runat="server"><img src="/images/icon/envelop.jpg" alt="Contact Us"><strong class="font-size-14">Reply</strong></asp:LinkButton>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
In the .aspx.cs file
public void rptNotice_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
lbMessage.Text = "MessageId: " + e.CommandArgument.ToObjectString();
}
There seems to be possible parsing issue due to unknown parameter. I would suggest using CommandArgument field. Try "replyModal" to CommandArgument .
Split the value at backend and get operation type.

Validation in Update Panel within a Repeater

I am using an Update Panel within a Repeater. I need for the Update Panel to contain validation. The validation is in place but is getting fired for each Panel. I need it to only be applied to the Panel being accessed.
<asp:Repeater ID="rptComments"
runat="server"
OnItemDataBound="rptComments_OnItemDataBound"
OnItemCommand="rptComments_OnItemCommand">
<HeaderTemplate>
<ul class="comments">
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:UpdatePanel ID="updatePanelReply" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:PlaceHolder ID="plcForm" runat="server">
<div class="errors">
<asp:ValidationSummary ID="nestedCommentsValidation" ValidationGroup="nestedSubmit" runat="server" cssClass="validation" ForeColor="Red" />
</div>
<label>Name<sup>*</sup></label>
<asp:TextBox ID="tbReplyName" runat="server" cssClass="styledInput"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfv_tbReplyName"
runat="server"
Display="None"
ControlToValidate="tbReplyName"
InitialValue=""
ErrorMessage="Please enter your name."
ValidationGroup="nestedSubmit">
</asp:RequiredFieldValidator>
<asp:Button ID="btnButton" runat="server" Text="Button" CssClass="submit-button" CommandName="SubmitReply" ValidationGroup="nestedSubmit" />
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Are you using different validation groups in your different panels? The whole point of the validation group is that the validators will only validate the controls that are in the same group.
I ended up taking out the validation group and handling it in the OnItemCommand event. There may be a slicker way of doing it, but this worked for me.
if (String.IsNullOrEmpty(tbReplyName.Text.ToString().Trim()))
strValidationMessage = "<li>Please enter your name.</li>";
Literal ltrErrorMessage = (Literal)e.Item.FindControl("ltrErrorMessage");
ltrErrorMessage.Text = strValidationMessage;

asp panel default button property is not working in ie for live application

I have a page having multiple buttons and search functionality.I have placed one button and textbox in a panel.After writing seach text if I hit Enter,it works well for all the browsers on local, but for live application this is not working in IE.
The html is:
<asp:Panel ID="pnlSearch" runat="server" CssClass="search" DefaultButton="btnSearch">
<h2>
Explore QualityStocks without logging in</h2>
<asp:TextBox ID="txtSearch" CssClass="txt" runat="server" ToolTip="Stock Ticker" ></asp:TextBox>
<asp:Button TabIndex="0" ID="btnSearch" runat="server" ClientIDMode="Static" CssClass="btn" OnClick="btnSearch_Click" />
</asp:Panel>
What am I missing to do?
Thanks in advance,
Priya

multiple submit buttons won't postback on aspx - only last button

On my aspx page I have two fieldsets, each wrapped with an <asp:Panel DefaultButton="..."> that have their own <input type="submit"> buttons. Here's an abbreviated version...
<div id="content">...</div>
<asp:Panel runat="server" id="formPanel1" DefaultButton="form1SubmitButton">
<fieldset>
<asp:TextBox runat="server" id="textbox1"/>
<asp:TextBox runat="server" id="textbox2"/>
<asp:Button runat="server" id="form1SubmitButton" OnClick="form1SubmitButton_OnClick"/>
</fieldset>
</asp:Panel>
<asp:Panel runat="server" id="formPanel2" DefaultButton="form2SubmitButton">
<fieldset>
<asp:TextBox runat="server" id="textbox3"/>
<asp:TextBox runat="server" id="textbox4"/>
<asp:Button runat="server" id="form2SubmitButton" OnClick="form2SubmitButton_OnClick"/>
</fieldset>
</asp:Panel>
Only the last submit button form2SubmitButton will postback, the other button does nothing when clicked. I have simplified the code above but I should mention that each form, Panel included, is kept in a separate user control that is registered on the aspx.
UPDATE: After some more research I've figured out that the submits are not working because of validators on the another fieldset on the master page. That fieldset is for signing into the site, so it is needed. Thoughts?
As PCasagrande mentioned, make sure your validation groups match the default button and all validators. Specially for your logging part.
Example:
<asp:Panel ID="pnlUpdateInfo" runat="server" DefaultButton="btnUpdateInfo">
<ASP:TEXTBOX id="txtZip" runat="server"></ASP:TEXTBOX>
<asp:RequiredFieldValidator id="reqvalZipSignUp" runat="server"
ControlToValidate="txtZip" ValidationGroup="btnUpdateInfo" />
<asp:Button ID="btnUpdateInfo" runat="server" ValidationGroup="btnUpdateInfo" />
</asp:Panel>
Turns out it has to do with HTML5 constraint validation. I figured this out because in chrome, from the console, I was getting the error An invalid form control with name='...' is not focusable. This led me to this post. I added the html5 attribute formnovalidate="formnovalidate" to the submit buttons and everything works just fine (*tested in chrome v20.0.1132.57, firefox v14.0.1, IE9 v9.0.8112).
So to recap, here's the answer...
<asp:Button runat="server" id="submit1" OnClick="submit1_OnClick" text="Submit" formnovalidate="formnovalidate"/>

Categories

Resources