Visibility issue of textbox when even its not created - c#

I have two textboxes, txtA and txtB, both are never shown on the display simultaneously, and in code behind when i am applying the condition to check the visibility of textbox, its coming to be true when the textbox is don't even created.
ASPX Code:
<%if(CurrentOrderItem.MasterModelName.ToLower().Contains("string1"))
{ %>
<div class="CustomerName clearfix ">
<div class="txtInput width464">
<asp:TextBox ID="txtA" runat="server" MaxLength="12" />
</div>
</div>
<%} %>
<%if (CurrentOrderItem.MasterModelName.ToLower().Contains("string2"))
{ %>
<div class="txtInput width464">
<asp:TextBox ID="txtB" runat="server" MaxLength="20" autocomplete="off"/>
</div>
</div>
<%} %>
And when i check into the codebehind visibility of the not created textbox is shown as true.

Note that, the server side code embedded in the aspx page will be excecuted during the Render phase. Which means all your controls are created and initialized with the values. So in all the events before Render, you get each control available in the codebehind with the data.
http://msdn.microsoft.com/en-us/library/vstudio/0e39s2ck(v=vs.100).aspx
Also as mentioned in the article, it is not good practice to embed server side code in the ASPX page, because it leads to difficult in maintainance and un expected result.
What you can do instead is that, in the code behind, in the page load you can check the values and make your controls visible or hidden

when i am applying the condition to check the visibility of textbox,
its coming to be true when the textbox is don't even created.
TextBox's Visible will always be true. Althought it is not visible in the browser, its information is stored ViewState.
Other thought
Placing the c# code in ASPX page is fragile and hard to maintain.
Instead, you can easily achieve the same result using Panel control which renders as html div tag.
<asp:Panel ID="APanel" runat="server" CssClass="CustomerName clearfix">
<div class="txtInput width464">
<asp:TextBox ID="txtA" runat="server" MaxLength="12" />
</div>
</asp:Panel>
<asp:Panel ID="BPanel" runat="server" CssClass="CustomerName clearfix">
<div class="txtInput width464">
<asp:TextBox ID="txtB" runat="server" MaxLength="20" autocomplete="off"/>
</div>
</asp:Panel>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (CurrentOrderItem.MasterModelName.ToLower().Contains("string1"))
{
APanel.Visible = true;
BPanel.Visible = false;
}
else
{
APanel.Visible = false;
BPanel.Visible = true;
}
}
}
In this approach, you can check Panel's Visible instead of TextBox.

Related

asp RadioButton not selected when Checked="true"

I have two radio buttons "Yes" and "No" and I want the "No" radio button to be selected by default. However, I can't get it be be selected through the asp code or by the code behind. Am I doing this completely wrong or did I just miss something.
ASP code:
<div class="query_header" runat="server" id="ScanOnStartup">
<div class="formlabel" style="width: 300px">Perform Scan on startup</div>
<div class="formfield" style="line-height: 10px">
<asp:RadioButton ID="scanOnStartupYes" Text="Yes" GroupName="ScanOnStartupRadio" runat="server" AutoPostBack="false" Checked="false"/>&nbsp
<asp:RadioButton ID="sanOnStartupNo" Text="No" GroupName="ScanOnStartupRadio" runat="server" AutoPostBack="false" Checked="true"/>
</div>
<div class="formdivider"></div>
</div>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.scanOnStartupNo.Checked = true;
}
If you need to make a radio button checked by default on page load , you can do one of the above
From the front end , you can add the property
Checked="checked"
From the code behind, you can write it as
scanOnStartupNo.Checked=true
When I checked your code , You have made a spelling mistake in declaring the ID of NO button. That was why the code behind code piece was not working
Okay aside from the typo "sanOnStartupNo", I know where the problem is coming from. There was actually another control from my code behind that renders the page and that is where the selection for the radio button is being set. I added in code for my radio button and it works now.

ASP.Net Repeater Responsive Controls Binding

I have ASP.Net controls for search attributes with check box options and ASP.NET repeater. For Responsive Design, I have same bindings in two different places to match the design.
Here, CheckBoxList ID=Options1 & ID=Options2 both having same binding but the code will repeat. I have 400 lines to check the conditions for single binding. i have to duplicate all the codes again for the another view. Is there any way to optimize the code to single bind with both views handling. ( Avoid duplicate binding and checking)?
// Desktop View
<div class="hidden-sm hidden-xs narrowSearch">
//Content
<asp:Repeater ID="rptAttributes1" runat="server" EnableViewState="true" OnItemDataBound="rptAttributes_ItemDataBound">
<ItemTemplate>
<li>
<div class="form">
<asp:CheckBoxList ID="Options1" runat="server" AutoPostBack="false" Visible="false"
DataTextField="EnOptionName" DataValueField="SubCategoryAttributeOptionID" Font-Strikeout="False" />
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
// Mobile view
<div class="sec_left hidden-lg hidden-md">
<div class="moremenu narrowSearch">
//Content
<asp:Repeater ID="rptAttribute2" runat="server" EnableViewState="true" OnItemDataBound="rptAttributes_ItemDataBound">
<ItemTemplate>
<li>
<div class="form">
<asp:CheckBoxList ID="Options2" runat="server" AutoPostBack="false" Visible="false"
DataTextField="EnOptionName" DataValueField="SubCategoryAttributeOptionID" Font-Strikeout="False" />
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
Code Behind:
CheckBoxList chklOptions1 = item.FindControl("Options1") as CheckBoxList;
CheckBoxList chklOptions2 = item.FindControl("Options2") as CheckBoxList;
You can place your repeater (including the declarative code as well as what you have in the code-behind of your page) into a custom user control. Then you put the user control in each section (desktop and mobile) of your page. This is the simplest way to avoid repeated code. Having said this, since you've only provided part of the code that's in the code-behind class of your page, I can't speak to what changes might be required when you move the imperative code you have in your page's code-behind class to the user control.
Good luck!

Show User Control through Static Method

I have been researching this, but haven't been able to find anything so far.
Currently, I am using a Javascript alert box on my ASP page:
public static class Alert
{
public static void Show(string message)
{
string script = "<script type=\"text/javascript\">alert('" + message + "');</script>";
Page page = HttpContext.Current.CurrentHandler as Page;
if (!page.ClientScript.IsStartupScriptRegistered("alert"))
{
page.ClientScript.RegisterStartupScript(typeof(Alert), "alert", script);
}
}
}
I am able to call this from code behind by: Alert.Show("Text");
My plan is to replace the Javascript alert by utilizing the AjaxControlToolkit's ModalPopupExtender. I am creating a user control that looks something like this:
<ajax:ModalPopupExtender ID="mpAlert" runat="server" PopupControlID=""
TargetControlID="btnExport" OkControlID="btnOK">
</ajax:ModalPopupExtender>
<asp:Panel ID="pnlSteps" runat="server" BackColor="#C5D9FC" Width="10%"
BorderColor="#093E9A" BorderStyle="Double" BorderWidth="5px"
style="border-radius: 10px; padding: 5px;">
<div>
<asp:Literal ID="lSteps" runat="server" />
</div>
<div>
<input id="btnOK" type="button" value="OK" />
</div>
</asp:Panel>
I am wondering if there is a way I can create a static method to show this from codebehind, similar to how I used the Javascript alert. I'd like to be able to wrap this in a class so I could just call AjaxAlert.Show(), without having to call anything in the aspx file.
This is still a rough idea, so if any more details are needed, just let me know.
You can show the ModalPopupExtender from codebehind. Therefore you need an invisible trigger button.
mpAlert.Show();
So set the TargetControlID of the ModalPopupExtender to a hidden button:
<asp:Button ID="Hid_ShowDialog" Style="display: none" runat="server" />

aspx page not load in iframe click on button

<asp:Button runat="server" ID="hiddenPopupPatientRegistration" Style="display: none" />
<asp:ModalPopupExtender ID="popupPatientRegistration" runat="server" PopupControlID="panelPatientRegistration"
TargetControlID="hiddenPopupPatientRegistration" BackgroundCssClass="cssmodalbackground"
BehaviorID="modalBehaviour" CancelControlID="btnClose">
</asp:ModalPopupExtender>
<div id="panelPatientRegistration" class="cssmodalpopup" style="display: none">
<iframe id="iframePatientRegistration" class="csstable" runat="server" width="600px"
height="485px" scrolling="auto"></iframe>
<table>
<tr>
<td align="right">
<asp:Button ID="btnClose" runat="server" CssClass="cssbutton" Text="Close" />
</td>
</tr>
</table>
</div>
protected void btn_Click(object sender, EventArgs e)
{
iframePatientRegistration.Attributes["src"] = "Patient_Registration.aspx?flowType=" + flowType + "&fromTime=" + hidFromTime.Value + "&toTime=" + hidToTime.Value + "&acqModalityID=" + hidAcqModalityID.Value + "&schLocationID=" + ddlScheduledProcedureStepLocation.SelectedValue;
popupPatientRegistration.Show();
}
I have to show aspx page in modal popup extender control.
And pass values through query string
BUT ASPX page not loads in Iframe.code behind.
when i see html source then getting these two lines
<HTML>
</HTML>
the display property for the containing Div "panelPatientRegistration" is set to none. The iframe is inside that and it never gets rendered.
Collect all your Querystring data and save them in hidden fields and write javascript function where change the src of the Iframe by passing the values as querystrings using Hidden fields.
<script>
function changeIframSrc(src) {
document.getElementById('<%=iframePatientRegistration.ClientID %>').src = src;
}
</script>
the problem i saw in the code is you are clicking a server side button where the page is getting posted back and even though you are changing the src of the iframe but controls are getting created again due to the postback. so use the javascript function above to call that particular page and pass the querystrings using the hidden fields solve the issue.

how to code on on image button if image button are in listview in asp.net C#

Here is a code tag code of asp.net
and there is a Image button where i want code on click event for download item
i try to code on click event but it was not working
<asp:ListView runat="server" ID="ListView1" GroupItemCount="1">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
</div>
</LayoutTemplate>
<GroupTemplate>
<div style="clear: both;">
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</div>
</GroupTemplate>
<ItemTemplate>
<div style="margin-left: 20px; border: 2px rgba(255, 102, 0, 0.36)
dotted; width: 900px; padding: 10px;">
<video src='<%# "Handler.ashx?id=" + Eval("id") %>'
width="900" height="400" controls="" preload=""></video>
</div>
<div style="width:950px; height:90px;" align="right">
<asp:ImageButton ID="ImageButton1"
ImageUrl="images/download-button.jpg" runat="server" width="230px"/>
</div>
</ItemTemplate>
<GroupSeparatorTemplate>
<div style="clear:both"></div>
</GroupSeparatorTemplate>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:ListView>
Here is a c# Private code class on same page that is
Private void Download(string ID)
{
some code here for download
}
please let me know how can i code on image button for download a file.
Error:
Invalid postback or callback argument.
Event validation is enabled using
<pages enableEventValidation="true"/> in configuration or
<%# Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that arguments to
postback or callback events originate from the server
control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
button control is inside listview why dont you give it Command-name and then run the Item-command of the listview.
e.g
Assign the CommandName property of the Image button and then in ItemCommand of listview write
<asp:ImageButton ID="ImageButton1" ImageUrl="images/download-button.jpg"
runat="server" width="230px" CommandName ="Download"/>
if (e.CommandName = "Download")
{
WriteYour Code here..
}
for accessing that button in listview you should use item command event like this:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "EditRecord")
{
}
else if (e.CommandName == "DeleteRecord")
{
}
}
you can also use the onclick event of button but that's not recommended way of doing. you have to manually add the OnClick="ImageButton1_OnClick()" in button markup and then put the event manually in the code behind file
protected void ImageButton1_OnClick(object sender, EventArgs e)
{
}
Regarding your Invalid postback or callback argument error, some html is generated by some control. you have to overview your whole webform, and register any script you are using under scriptmanager. or if nothing works out you can disable this check. <%# Page EnableEventValidation="false" %>

Categories