List items disappear after postback - asp.net

I am having lots of problems with my dropdownlist. My dropdownlist controls disappear after postback.
The idea is that if in the drop down list I choose Spain, the ddlProvincia control shows the Spanish provinces. However, if I choose any country other than Spain, I should show the textbox "tbProvinciaNombre".
The problem is that when I select a country in the "ddlPais" control the list items of this control and "ddlProvincia" disappear completely.
Any help is welcome.
Thank you.
<asp:UpdatePanel ID="upProvinciaNombre" runat="server" UpdateMode="Conditional" class="col-md-6 form-group">
<ContentTemplate>
<!-- Provincia -->
<div id="panelDdlProvincia" runat="server" visible="true">
<asp:Label runat="server" AssociatedControlID="ddlProvincia">Provincia</asp:Label><span class="requerido">*</span>
<asp:DropDownList ID="ddlProvincia" runat="server" CssClass="form-control selectpicker" />
<asp:RequiredFieldValidator ID="rfvddlProvincia" runat="server" ErrorMessage="Seleccione una opción" CssClass="invalid-feedback" Text="*" InitialValue="none" ControlToValidate="ddlProvincia" ValidationGroup="ValidationButton"></asp:RequiredFieldValidator>
<ajax:ValidatorCalloutExtender ID="vceRfvddlProvincia" runat="server" TargetControlID="rfvddlProvincia" HighlightCssClass="error" CssClass="oculto" />
</div>
<!-- Provincia Nombre -->
<div id="panelTbProvinciaNombre" runat="server" visible="false">
<asp:Label runat="server" ID="lbProvinciaNombre" AssociatedControlID="tbProvinciaNombre">Provincia</asp:Label><span class="requerido">*</span>
<asp:TextBox ID="tbProvinciaNombre" runat="server" CssClass="form-control"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="rfvTbProvinciaNombre" runat="server" CssClass="invalid-feedback" ControlToValidate="tbProvinciaNombre" Display="Dynamic" ValidationGroup="ValidationButton" ErrorMessage="Introduzca una provincia válida" />
<ajax:ValidatorCalloutExtender ID="vceRfvTbProvinciaNombre" runat="server" TargetControlID="rfvTbProvinciaNombre" HighlightCssClass="error" CssClass="oculto" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<!-- Pais -->
<asp:UpdatePanel ID="upPais" runat="server" UpdateMode="Conditional" class="col-md-6 form-group">
<ContentTemplate>
<asp:Label runat="server" AssociatedControlID="ddlPais">Pais</asp:Label><span class="requerido">*</span>
<asp:DropDownList ID="ddlPais" runat="server" CssClass="form-control selectpicker" AutoPostBack="true" />
<asp:RequiredFieldValidator ID="rfvddlPais" runat="server" ErrorMessage="Seleccione una opción" CssClass="invalid-feedback" Text="*" InitialValue="none" ControlToValidate="ddlPais" ValidationGroup="ValidationButton"></asp:RequiredFieldValidator>
<ajax:ValidatorCalloutExtender ID="vceRfvddlPais" runat="server" TargetControlID="rfvddlPais" HighlightCssClass="error" CssClass="oculto" />
</ContentTemplate>
</asp:UpdatePanel>
<!-- Políticas -->
<div class="col-md-12 form-group">
<div class="checkbox">
<asp:CheckBox ID="cbPoliticas" runat="server" />
<asp:Label runat="server" ID="politicas" AssociatedControlID="cbPoliticas">He leído y acepto la
<asp:HyperLink ID="hlkPoliticaPrivacidad" runat="server" Target="_blank" NavigateUrl="~/contenidos/privacidad.aspx" Text="política de privacidad" />
y el
<asp:HyperLink ID="hlkAvisoLegal" runat="server" Target="_blank" NavigateUrl="~/contenidos/avisoLegal.aspx" Text="aviso legal" />
del sitio web.
</asp:Label>
<div>
<asp:TextBox ID="tbFakeCv" runat="server" CssClass="oculto" />
<asp:CustomValidator runat="server" ID="cvCbPoliticas" ErrorMessage="Es obligatorio aceptar la política de privacidad" CssClass="error" Display="Dynamic" ControlToValidate="tbFakeCv" ValidateEmptyText="true" ValidationGroup="ValidationButton" />
<ajax:ValidatorCalloutExtender ID="vceCvCbPoliticas" runat="server" TargetControlID="cvCbPoliticas" HighlightCssClass="error" CssClass="oculto" />
</div>
</div>
</div>

The solution was really simple. In my case, I was using the class "selectpicker", but I assumed that this functionality was implemented in the .js file. But no, It was missing. Just add the following code in the .js file and everything is working like a charm.
Sorry. I just want to comment it because sometimes the frontend and backend are responsible of different people, and a error in one part could be a pain in the ass to the other member of the team.
// Selectpicker
if ($.fn.selectpicker) {
$('.selectpicker:not(.jQ_todos)').selectpicker();
$('.selectpicker.jQ_todos').selectpicker({ noneSelectedText: 'Todos' });
}

Related

Finding the datakey of a nested listview control

I have a listview control that is bounded by an entity query. The main listview is bounded to the entity called Article. The second listview(nested one) is bounded to an icollection of Article_Comment. I am trying to get the ID of a article so that I can use it in updating the article comment table. Below is what I have so far.
<asp:ListView ID="listComment" runat="server" DataKeyNames="ArticleID" >
<LayoutTemplate>
<div class="row">
<h3>Comments</h3>
<blockquote>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</blockquote>
</div>
</LayoutTemplate>
<ItemSeparatorTemplate>
<hr />
</ItemSeparatorTemplate>
<ItemTemplate>
<h4 class="text-error"><%#Eval("Title")%></h4>
<br />
<h4 class="text-error"><%#Eval("ArticleID")%></h4
<br />
<p><%#Eval("ArticleContent")%> </p>
<asp:ListView ID="list" runat="server" DataSource='<%# Eval("Article_CommentTable")%>'
InsertItemPosition="LastItem" OnItemCommand="list_ItemCommand" OnItemDataBound="list_ItemDataBound"
DataKeyNames="ArticleID" OnItemInserting="list_ItemInserting">
<LayoutTemplate>
<div class="row">
<h3>Comments</h3>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<p>
Username : <%#Eval("UserName")%>
<br />
Comments : <%#Eval("Comment")%>
<br />
<%--<asp:Label ID="lblID" runat="server" Text='<%# Bind("ArticleID")%>'></asp:Label>--%>
<asp:HiddenField ID="hid" runat="server" Value='<%# Bind("ArticleID")%>' />
</p>
</ItemTemplate>
<InsertItemTemplate>
<div class="row" runat="server">
<asp:TextBox ID="txtUserName" runat="server" CssClass="form-control" />
<br />
<asp:TextBox ID="txtComment" runat="server" CssClass="form-control" />
<FCKeditorV2:FCKeditor ID="editorArticle" runat="server"
BasePath="~/FCKeditor/" Height="200px" Width="400px"
Value="Start typing here" ToolbarStartExpanded="False">
</FCKeditorV2:FCKeditor>
<br />
<asp:Button ID="btnAddComment" runat="server" CssClass="btn btn-info"
CommandName="insert" Text="Join The Discussion" CommandArgument='<%#Eval("ArticleID")%>' />
</div>
</InsertItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
I am trying to access the ArticleID for each row that a button is clicked. How should I do that?
you can get it in ItemCommand event as listComment.DataKeys[dataItem.DisplayIndex].Value

RadioButtons causing built-in .net form validation to malfunction

I have a very basic form in a .Net page, it has 6 fields each with a requiredfieldvalidator attached. Everything works fine and the form is stopped if even one field is blank. I was asked to add a section with 2 radio buttons (I set one option as checked to avoid any validation on the field), now the form submits no matter if the fields have information in them or not.
Subset of default.aspx
<form id="form1" runat="server">
<div style="margin-bottom:10px;">
<label for="txtAgencyName">Name of Agency</label>
<asp:TextBox ID="txtAgencyName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAgencyName" runat="server" ErrorMessage="*Required" ControlToValidate="txtAgencyName" ValidationGroup="vg1" CssClass="rfvErrors"></asp:RequiredFieldValidator>
</div>
<div style="margin-bottom:10px;">
<label for="txtEmail">Email Address</label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="*Required" ControlToValidate="txtEmail" ValidationGroup="vg1" CssClass="rfvErrors"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmail" runat="server" ErrorMessage="Invalid Email Address" CssClass="revErrors" ValidationExpression="(^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$)" ValidationGroup="vg1" ControlToValidate="txtEmail"></asp:RegularExpressionValidator>
</div>
<!-- This is the section that is breaking the validation -->
<div style="margin-bottom:10px;">
<asp:RadioButton ID="rbOwnershipAgent" runat="server" Checked="true" GroupName="Ownership" />
<asp:RadioButton ID="rbOwnershipReferral" runat="server" GroupName="Ownership" />
</div>
<!-- --------------------------------------------------- -->
<div style="margin-bottom:10px;">
<label for="txtAgencyContact">Agency Contact Name</label>
<asp:TextBox ID="txtAgencyContact" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAgencyContact" runat="server" ErrorMessage="*Required" ControlToValidate="txtAgencyContact" ValidationGroup="vg1" CssClass="rfvErrors"></asp:RequiredFieldValidator>
</div>
<div style="margin-bottom:10px;">
<label for="txtPhone">Telephone Number</label>
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTelephone" runat="server" ErrorMessage="*Required" ControlToValidate="txtPhone" ValidationGroup="vg1" CssClass="rfvErrors"></asp:RequiredFieldValidator>
</div>
<div>
<label for="ddlState">State</label>
<asp:DropDownList ID="ddlState" runat="server" AppendDataBoundItems="True">
<asp:ListItem Text="Select State" Value=""></asp:ListItem>
</asp:DropDownList>
</div>
<div style="margin-bottom:10px;">
<label for="taCarriers">In your agency, who are the carriers writing liquor liability for you today?</label>
<asp:TextBox ID="taCarriers" runat="server" TextMode="MultiLine" Rows="3"></asp:TextBox><asp:RequiredFieldValidator ID="rfvOtherCarriers" runat="server" ErrorMessage="*Required" ControlToValidate="taCarriers" ValidationGroup="vg1" CssClass="rfvErrors" style="margin-top:15px;"></asp:RequiredFieldValidator>
</div>
<div style="width:100%;margin-top:20px;border-bottom:1px solid #27a0d6"></div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit »" name="bnSubmit" class="submit" ValidationGroup="vg1" />
</form>
Am I missing something simple as far as an attribute on the submit button or radio buttons? I'm just surprised that a built in .Net control is causing this kind of trouble for something this basic. Thanks for any help.

how to dynamically assign id to controls - controls are inside datalist in asp .net

I am using datalist for showing datas. Datas are test questions with 4 options.I have 50 questions displaying in datalist.
By using ItemDataBound event - i am making visible only one question with 4 options to user. Remaining questions are invisible . In the Itemdatabound event - i am assigning id to the radio buttons. but also id is not assigned to the datalist ,it showing different id.
<div class="widget">
<div class="widget-header" align="center" style="background: #4a98c9; color: #FFFFFF;">
<h4>
<asp:Label ID="Label5" runat="server" Text="Question : "></asp:Label>
<asp:Label ID="lblQuestionNo" runat="server" Text="0 of 0"></asp:Label></h4>
<asp:HiddenField ID="HdCurrentQuestSlNo" runat="server" />
<asp:HiddenField ID="HdfMinTime" runat="server" />
<asp:HiddenField ID="HdfSecTime" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HdfQSlNo" runat="server" />
<asp:HiddenField ID="HdQuestCount" runat="server" Value="0"/>
</div>
<div class="widget-content">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="PanelQuestBlock" runat="server" ScrollBars="Vertical" Width="100%" Height="325px">
<asp:DataList runat="server" ID="DataListQuestion"
onitemdatabound="DataListQuestion_ItemDataBound"
onitemcreated="DataListQuestion_ItemCreated">
<ItemTemplate>
<div class="questdisplayId" id="divQuestBlock" runat="server">
<div>
<asp:HiddenField ID='HdSlNo' runat="server" Value='<%# Eval("SlNo") %>' ClientIDMode="Static"/>
<asp:HiddenField ID="HdUserQuestId" runat="server" Value='<%# Eval("UserQuestionId") %>' ClientIDMode="Static"/>
<asp:HiddenField ID="HdTotalOptions" runat="server" Value='<%# Eval("TotalOptions") %>' ClientIDMode="Static"/>
<asp:HiddenField ID="HdQuestAnsweredTimeSecs" runat="server" Value='<%# Eval("AnsweredTimeSecs") %>' ClientIDMode="Static"/>
<asp:HiddenField ID="HdOptionAnswered" runat="server" Value='<%# Eval("OptionAnswered") %>' ClientIDMode="Static"/>
</div>
<div>
<asp:Label ID="Question" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
</div>
<div class="answer-row" id="divOption1" runat="server">
<asp:RadioButton ID="Option1" runat="server" GroupName="Answer" value="1" Text='<%# Eval("Option1") %>'
CssClass="rdbtn" Style="vertical-align: middle" ClientIDMode="Static"/>
<asp:Label ID="lblOpt1" runat="server" Text=""></asp:Label>
</div>
<div class="answer-row" id="divOption2" runat="server">
<asp:RadioButton ID="Option2" runat="server" GroupName="Answer" value="2" Text='<%# Eval("Option2") %>'
CssClass="rdbtn" Style="vertical-align: middle" ClientIDMode="Static"/>
<asp:Label ID="lblOpt2" runat="server" Text=""></asp:Label>
</div>
<div class="answer-row" id="divOption3" runat="server">
<asp:RadioButton ID="Option3" runat="server" GroupName="Answer" value="3" Text='<%# Eval("Option3") %>'
CssClass="rdbtn" Style="vertical-align: top; left: auto;" ClientIDMode="Static"/>
<asp:Label ID="lblOpt3" runat="server" Text=""></asp:Label>
</div>
<div class="answer-row" id="divOption4" runat="server">
<asp:RadioButton ID="Option4" runat="server" GroupName="Answer" value="4" Text='<%# Eval("Option4") %>'
CssClass="rdbtn" Style="vertical-align: text-top" ClientIDMode="Static"/>
<asp:Label ID="lblOpt4" runat="server" Text=""></asp:Label>
</div>
<div class="answer-row" id="divOption5" runat="server">
<asp:RadioButton ID="Option5" runat="server" GroupName="Answer" value="4" Text='<%# Eval("Option5") %>'
CssClass="rdbtn" Style="vertical-align: text-top" ClientIDMode="Static"/>
<asp:Label ID="lblOpt5" runat="server" Text=""></asp:Label>
</div>
</div>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<div class="row">
<div class="span8" align="center">
<asp:Button ID="btnClear" runat="server" Text="Clear Selection"
CssClass="btn btn-warning" OnClientClick="Clearbutton();"
/>
<asp:Button ID="btnSave" runat="server" Text="Save & Next" CssClass="btn btn-success" OnClientClick="Savebutton()"/>
</div>
<div class="span4" align="center">
<asp:Button ID="btnReview" runat="server" Text="Mark Review & Next" CssClass="btn btn-info" OnClientClick="Reviewbutton()"/>
<asp:Button ID="btnFinish" runat="server" Text="Finish" CssClass="btn btn-danger" OnClientClick="Finishbutton()"/>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnReview" />
<asp:AsyncPostBackTrigger ControlID="btnClear" />
<asp:AsyncPostBackTrigger ControlID="btnSave" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DataListQuestion" />
</Triggers>
</asp:UpdatePanel>
</div>
</div>
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HiddenField HdSlNo = (HiddenField)e.Item.FindControl("HdSlNo");
RadioButton Option1 = (RadioButton)e.Item.FindControl("Option1");
RadioButton Option2 = (RadioButton)e.Item.FindControl("Option2");
RadioButton Option3 = (RadioButton)e.Item.FindControl("Option3");
RadioButton Option4 = (RadioButton)e.Item.FindControl("Option4");
RadioButton Option5 = (RadioButton)e.Item.FindControl("Option5");
Option1.ID = "rdbtnoption1_" + HdSlNo.Value.ToString();
Option2.ID = "rdbtnoption2_" + HdSlNo.Value.ToString();
Option3.ID = "rdbtnoption3_" + HdSlNo.Value.ToString();
Option4.ID = "rdbtnoption4_" + HdSlNo.Value.ToString();
Option5.ID = "rdbtnoption5_" + HdSlNo.Value.ToString();
}
In my design page code , using Datalist i am binding 50 questions . while binding itself i am hiding 2 to 50 questions . Question 1 only visible. I tried to give dynamic id for every questions and options using event databound and rowcreated. First it got error , after that i changed properties as ClientIDMode="Static" for controls.it shown different id. But if i click any button event again it clear the id's.again it shows old ids only.

asp.net ModalPopupExtender not behaving properly

I have the following code on a .aspx file. If I comment out the ModalPopupExtender code works fine.:
<cc1:ModalPopupExtender BehaviorID="mdlPopup" runat="server" TargetControlID="btn"
ID="mdl1" PopupControlID="pnlPopup" OkControlID="LinkButton1" BackgroundCssClass="modalBackground" />
<asp:Panel ID="pnlPopup" runat="server" CssClass="confirm-dialog" Style="display: none;">
<div class="inner">
Work Order #: <asp:TextBox ID="txtWorkOrder" runat="server" Width="285" Text=""></asp:TextBox><br /><br />
Please Enter Corrective Actions<br />
<asp:TextBox ID="TextBox2" runat="server" Height="150" Width="285" TextMode="MultiLine"
Text=""></asp:TextBox>
<asp:Button ID="btnOK" runat="server" Font-Bold="true" Text="Submit" OnClick="click"
Width="150" />
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="close" OnClick="cancel" />
</div>
</asp:Panel>
<asp:Button ID="btn" runat="server" Style="display: none;" />
<asp:Label ID="id" runat="server" Text="Label" Visible="false"></asp:Label>
The problem that I am having is that the page just hangs when I have this code in the .aspx file. Is there anything I should do. It does not even get to the Page_Load.
I am calling this page that has the above code with the following:
string url = "Alerts.aspx?";
url += "ID=" + id.Text;
Response.Redirect(url);

Aligning .aspx buttons

I have an update panel that contains a label, a dropDownList and two buttons:
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="dropDownList" style="position:relative;" runat="server">
<label>
Select New File: </label>
<div id="ddl" runat="server">
<asp:DropDownList runat="server" ID="ddlCaseFiles" DataSourceID="dsMyCaseFiles" DataTextField="Display"
DataValueField="FileID" OnPreRender="ddl_PreRender" Width="524px" OnSelectedIndexChanged="ddlCaseFiles_SelectedIndexChanged" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlCaseFiles"
ToolTip="Casefile Required" InitialValue="-1" Text="*" Display="Dynamic" />
<ajaxToolkit:ListSearchExtender ID="ddlExtCaseFiles" runat="server" PromptCssClass="ListSearchExtenderPrompt"
TargetControlID="ddlCaseFiles" BehaviorID="ddlExtCaseFiles" Enabled="True" />
</div>
<br />
<asp:Button ID="btnMoveCaseFile" runat="server" Text=""
style="float:left;" onclick="btnMoveCaseFile_Click"/>
<asp:Button ID="btnCancel" runat="server" Text="Cancel"
style="float:right" onclick="btnCancel_Click"/>
<br />
</div>
<asp:Label runat="server" ID="lblStatus"></asp:Label>
</ContentTemplate>
I am trying to get my buttons to line up like this:(button at the beginning of the DropDownList and a button at the End)
I've tried float-absolute etc.
I figured it out. Seems as though you have to add in px after the number.....
<asp:Button ID="btnMoveCaseFile" runat="server" Text=""
style="position:absolute; float:left; margin-left:104px;" onclick="btnMoveCaseFile_Click"/>
<asp:Button ID="btnCancel" runat="server" Text="Cancel"
style="position:relative; float:right; margin-right:63px;"
since I had margin-right:63; instead of margin-right:63px;

Resources