include aspx file in other aspx file - c#

<% if (Session["desig"].ToString() == "Developer"){%>
<td>
<select name='zone' id="zone" onchange="showLoc(this.value,'mlodg_loc')">
<option value="Select Zone">Select Zone</option>
<option value="East">East</option>
<option value="West">West</option>
<option value="North">North</option>
<option value="South1">South1</option>
<option value="South2">South2</option>
<option value="South3">South3</option>
</select>
</td>
<%}
else
{%>
<td>
<select name='zone' id="Select1" onchange="showLoc(this.value,'mlodg_loc')">
<option value="Select Zone">Select Zone</option>
<option value="<%#Session["zone"]%>"><%# Session["zone"].ToString() %></option>
</select>
</td>
<%}%>
the code above is working fine if I am writing it directly, I tried to write this code in other file and in main file i did Response.WriteFile("zone.aspx")
How can i include it is there any way to include and also would like to know a better way to write the above statements.
Thankyou

UserControls are there in asp.net for this purpose, make a .ascx file for this piece of code u have, and use it as a control anywhere.

Response.WriteFile just writes the filecontents into the Http response stream, without parsing it.
Although it is possible to use Server Side includes in asp.net (<!--#include file="xxx.ext" -->), this has an asp-classic code smell to it IMO.
The better way to get re-use of .aspx components is by using a User Control (.ascx).
Similar question here
Update
Response.WriteFile is fine for plain Html, .css or .js, but not for code containing c# or referencing .Net objects (you've used Session in your code), e.g.
Response.WriteFile("HtmlSnippet.html")
where HtmlSnippet.html is
<p>In breaking news, scientists have discovered a planet in
<a href='http://en.wikipedia.org/wiki/Alpha_Centauri_Bb'>Alpha Centauri</a>
</p>
Using a Server Side Include, e.g. <!--#include file="file.inc" --> would allow you to do put something like this in file.inc:
<% if (Session["desig"].ToString() == "Developer"){ %>
You are a Developer!!
<% } %>
However, the recommendation is to use a User Control (.ascx), which allows you to do much the same as the server side include, but just in a much more reusable, object oriented, and testable fashion. Your reusable Control will be a first class object, with properties, methods and be able to raise events. You'll need to do some research here.

Response.WriteFile does not process the code on the server side. It simply takes the contents of html and pipes it to the browser.
If your code contains code blocks that must be processed by the server you can use this handy feature:
<div ID="menuContent" runat="server">
<!-- #Include virtual="/menu.aspx" -->
</div>
In my menu.aspx file I have raw html and some C# codeblocks and ASP will resolve those after inserting the content into the page. Great huh?

Related

Get attributes of a Web Element

Is there any technique that can be used to get attributes of an element in a web page? I'm trying to get properties of elements such as Links, Buttons, check boxes and Radio Buttons for test automation purposes to make it more easier. But I couldn't find any solution. I heard that CODEDUI can be used. If so, how can I implement it?
As you have mentioned html tag in your question, one solution is using jQuery like this:
$( elemement ).attr( "attribute" )
you do not get property of html page with program language php and c# you should useing jquery and Similar libraries or JavaScript .
for this problem you must using this code in select html with jquery :
<select id="selectVehicle">
<option value="1" data-year="2011">Mazda</option>
<option value="2" data-year="2015">Honda</option>
<option value="3" data-year="2008">Mercedes</option>
<option value="4" data-year="2005">Toyota</option>
</select>
$("#selectVehicle").change(function () {
alert($(this).find(':selected').data("year"));
});
or get attribute of elements :
$(this).attr('data-fullText');

ASP MVC3 - Marking Option as Selected

I have a form that has 2 select boxes (Country and State) that have their available options contained in partial HTML files, due to their size.
In the event of a validation error, I want to mark the option the user originally selected as "selected".
Since the options are contained in a partial HTML file, I am not sure that this is even possible:
Any suggestions would be great. Thanks
// Inside the View
<select name="State" id="State">
#Html.Partial("States", Model);
</select>
#Html.ValidationMessageFor(model => model.State)
The partial simply contains the HTML:
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="AA">Armed Forces Americas</option>
<option value="AE">Armed Forces Europe, Middle East</option>
[...]
Instead of using strange Partial, why you just do not use helper DropDownList. You can find many examples, like this one http://peternewhook.com/2013/02/asp-net-mvc-selectlist-selectedvalue-dropdownlistfor/

post a <FORM> to sharepoint page

I am doing following in one of my webpart .ascx file.
<FORM action="https://illustration.sagicorlifeusa.com/fse5/main/FormPost.aspx" id="frmLogin" method="post" target=blank>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<%= CompleteRequest %>
<input type="submit" name="__exclude__Submit" value="Run Sagicor Life Illustration Software Online" />
Notice the method="post"; However, looks like when I add this web-part, The hosting page already has a <FORM/> . How can I do the above POST?
posting to a new window from inside an ASPX web form (which SharePoint is) is tricky, because the framework wraps everything in a <form> it keeps track of. If you are required to post to the target page (i.e. get isn't good enough), you might have to:
create a separate .aspx page entirely to produce the form
open that form from within your Web Part with a JS call to window.open or an <a href="..." target="blank"
auto-post that form, with JavaScript, as soon as it loads.
(I have no firsthand experience with sharepoint; take this answer with a grain of salt.)

Using databinding to populate a HtmlSelect with optgroup elements in asp.net

I am using asp.net and have a HtmlSelect element on my page (with runat="server"). Normally I will just set the DataSource to some loaded data and DataBind. However in this case the data has one level of hierarchy and I want to represent this in the HTML with an optgroup. Google hasn't come up with any joy - is this even possible?
I've had a similar problem to solve and this is how I did it. Before I continue I did find all sorts of re-fracturing methods for lists, which probably do work. However, I did not think this the best route to go for my solution. Here's what I did:
I also needed to know which option a user selected in the drop down list but found that you can't build drop down list with option groups in ASP.net. So to do this I just built a simple <select> with <optgroup>'s and <option>'s in each group. Then I placed a <div> with a hidden textbox, which had the runat="server" set. I set the "onchange" event, on the select, to change the text value of the hidden textbox to whatever the value the option was, which was selected by the user, using javascript. Then, when the post back occurs, the code behind had access to the value the user selected, via the hidden textbox's runat="server". Problem solved! The code looks something like this:
Code in the HTML (aspx):
<div id="selectDiv">
<select id="selectItems" onchange="document.getElementById('hiddenselectDiv').getElementsByTagName('input')[0].value = this.options[this.selectedIndex].value;">
<optgroup label="Johannesburg">
<option value="Wilropark">Wilropark</option>
<option value="Bryanpark">Bryanpark</option>
<option value="Hurlingham">Hurlingham</option>
<option value="Midrand ">Midrand </option>
<option value="Glenvista">Glenvista</option>
<option value="Sunninghill">Sunninghill</option>
<option value="Edenvale ">Edenvale </option>
<option value="Parkhurst">Parkhurst</option>
</optgroup>
<optgroup label="Cape Town">
<option value="Tokai">Tokai</option>
<option value="Durbanville">Durbanville</option>
</optgroup>
<optgroup label="Durban">
<option value="Musgrave">Musgrave</option>
</optgroup>
<optgroup label="Pretoria">
<option value="Hatfield">Hatfield</option>
</optgroup>
</select>
</div>
<div id="hiddenSelectDiv">
<!--
Note: You probably want to set the hidden value to the first item you have seleced when you build the <select> object.
-->
<input type="hidden" id="selectedItem" value="Wilropark">
</div>
In the Code behind (C#):
if (!string.IsNullOrEmpty(selectedItem.Value))
{
//validation or whatever you want....
}
I've been looking around for the same thing, and it doesn't look like ASP supports this natively. One Google search found someone recommending a library at http://www.codeplex.com/SharpPieces, but I've never used it and I don't know how good it is. Other people talk about writing your own renderer for optgroup support.

Call JQuery from C# PageLoad

Is there any way how to call JQuery function from PageLoad C# file?
Basically I have some selects, some inputs which are not generated by C# code but are defined in .aspx file manually. When I send form get query to another page I would like to set same variables that are defined in querystring. I know how to do that when I use runat="Server" but I want pure JQuery solutions without having runat="server" objects.
Example:
Select input:
<form method="get" action="/list/search">
<select id="txtSearchFullTyp" name="typ">
<option value="all">Sell, Rent</option>
<option value="1">Sell</option>
<option value="2">Rent</option>
<select>
</form>
Now after sending querystring to another page I am analyzing querystring and running function with defined parameter.
I want to be able to set form to querystring defined as "typ". I dont know how to do it from C# when I dont have runat="Server" option.
Is there a way to do this?
Thanks.
Here's a code for it. It's quite hacky but nevertheless it answers your question:
<form method="get" action="/list/search">
<select id="txtSearchFullTyp" name="typ">
<option value="all">Sell, Rent</option>
<option value="1">Sell</option>
<option value="2">Rent</option>
<select>
</form>
<asp:Literal runat="server" ID="litJqueryCode"/>
and from the code behind you can do the following
litJqueryCode.Text = "<script type='text/javascript'>$(function(){ $('#txtSearchFullTyp').val('"+ Request.QueryString["dropdownValue"] +"') })</script>";

Categories