post data using get (not post) method - asp.net

I have a Webform with masterpage(where the form tag is). Inside the content page I have a textbox and a submit button. I want to send the data to the next page using GET not POST. How can I do this?

If it can apply to all pages:
<form method="get" ... >
<!-- content here -->
</form>
If you want only for a single, simple page:
protected void Submit_Click(object sender, EventArgs e)
{
string url = "NextPage.aspx?";
url = url + "&MyTxt1=" + MyTxt1.Text;
url = url + "&MyTxt2=" + MyTxt2.Text;
url = url + "&MyTxt3=" + MyTxt3.Text;
// etc.
Response.Redirect(url);
}
If you want to control the method from the content page:
// on the master page
public class SiteMaster : System.Web.UI.MasterPage
{
// replace form1 with the id of your form control
// make sure the form tag has runat="server"
public string Method
{
get { return form1.Method; }
set { form1.Method = value; }
}
// ...
}
// on the content page
protected void Page_Load(object sender, EventArgs e)
{
// replace SiteMaster with class type of the master class
((SiteMaster)this.Master).Method = "get";
}

Related

Sending information in url parameters using XmlHttpRequest in get method, how do i retrieve those parameters in aspx page?

I want to retrieve those two parameters which are passed in url
// Read the text file with an XMLHttpRequest
var xh;
if (window.XMLHttpRequest) {
xh = new XMLHttpRequest();
alert("Object created");
}
alert(xh);
xh.onreadystatechange = function() {
if (xh.readyState == 4 && xh.status == 200) {
alert(xh.responseText);
document.getElementById("d1").innerHTML = xh.responseText;
}
}
xh.open("GET", "Default.aspx?name=Henry&lname=Ford"", true);
xh.send();
</script>
Now i want to retrieve those two parameters in aspx page.
Code in aspx page-
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now);
}
}
You need to get that using QueryString as GET this is get request , params are passed using QueryString. For this url , you have to use following for in page_load to retrive.
//Default.aspx?name=Henry&lname=Ford
protected void Page_Load(object sender, EventArgs e)
{
string name,lname;
if(Request.QueryString["name"]!=null)
name = Request.QueryString["name"].ToString();
if (Request.QueryString["lname"] != null)
lname = Request.QueryString["lname"];
}

How to assign the value of user control property to aspx or codebehind in VB?

I have a user control with a public property which updates each time when a date from my calender(part of user control) is selected. Now I need to bring this value to page on which this user control is kept. How to do this.
I tried bringing the property value on page load event of aspx.vb(master page on which user control is present) but couldn't do it as page load is happening first and user property is loading next(null reference exception).
i tried this on page load of aspx hdnPPSeq.Value = PPCalender1.test1.ToString
Please share ideas to bring this value to aspx or codebehind in vb.
Create a method in usercontrol, which will return property value.
public partial class PassPropertyToPage : System.Web.UI.UserControl
{
string strSelectDateTime;
public string SelectedDateTime
{
set { strSelectDateTime = value; }
get { return strSelectDateTime; }
}
protected void Page_Load(object sender, EventArgs e){}
public string GetDateTime()
{
strSelectDateTime = <calendar value>;
return strSelectDateTime;
}
}
And in page, call the method to get the value:
public partial class AccessUserControlProperty : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("DateTime selected in page: " + PassPropertyToPage1.GetDateTime() + "<br/>");
}
}

how to access master page control from content page

I have a master page which contains a label for status messages. I need to set the status text from different .aspx pages. How can this be done from the content page?
public partial class Site : System.Web.UI.MasterPage
{
public string StatusNachricht
{
get
{
return lblStatus.Text;
}
set
{
lblStatus.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
I have tried this, but was unsuccessful in making it work:
public partial class DatenAendern : System.Web.UI.Page
{
var master = Master as Site;
protected void Page_Load(object sender, EventArgs e)
{
if (master != null)
{
master.setStatusLabel("");
}
}
protected void grdBenutzer_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//some code
if (master != null)
{
master.setStatusLabel("Passwort erfolgreich geändert.");
}
}
catch (Exception ex)
{
if (master != null)
{
master.setStatusLabel("Passwort konnte nicht geändert werden!");
}
}
}
}
}
In the MasterPage.cs file add the property of Label like this:
public string ErrorMessage
{
get
{
return lblMessage.Text;
}
set
{
lblMessage.Text = value;
}
}
On your aspx page, just below the Page Directive add this:
<%# Page Title="" Language="C#" MasterPageFile="Master Path Name"..... %>
<%# MasterType VirtualPath="Master Path Name" %> // Add this
And in your codebehind(aspx.cs) page you can then easily access the Label Property and set its text as required. Like this:
this.Master.ErrorMessage = "Your Error Message here";
In Content page you can access the label and set the text such as
Here 'lblStatus' is the your master page label ID
Label lblMasterStatus = (Label)Master.FindControl("lblStatus");
lblMasterStatus.Text = "Meaasage from content page";
It Works
To find master page controls on Child page
Label lbl_UserName = this.Master.FindControl("lbl_UserName") as Label;
lbl_UserName.Text = txtUsr.Text;
I have a helper method for this in my System.Web.UI.Page class
protected T FindControlFromMaster<T>(string name) where T : Control
{
MasterPage master = this.Master;
while (master != null)
{
T control = master.FindControl(name) as T;
if (control != null)
return control;
master = master.Master;
}
return null;
}
then you can access using below code.
Label lblStatus = FindControlFromMaster<Label>("lblStatus");
if(lblStatus!=null)
lblStatus.Text = "something";
You cannot use var in a field, only on local variables.
But even this won't work:
Site master = Master as Site;
Because you cannot use this in a field and Master as Site is the same as this.Master as Site. So just initialize the field from Page_Init when the page is fully initialized and you can use this:
Site master = null;
protected void Page_Init(object sender, EventArgs e)
{
master = this.Master as Site;
}
This is more complicated if you have a nested MasterPage. You need to first find the content control that contains the nested MasterPage, and then find the control on your nested MasterPage from that.
Crucial bit: Master.Master.
See here: http://forums.asp.net/t/1059255.aspx?Nested+master+pages+and+Master+FindControl
Example:
'Find the content control
Dim ct As ContentPlaceHolder = Me.Master.Master.FindControl("cphMain")
'now find controls inside that content
Dim lbtnSave As LinkButton = ct.FindControl("lbtnSave")
If you are trying to access an html element: this is an HTML Anchor...
My nav bar has items that are not list items (<li>) but rather html anchors (<a>)
See below: (This is the site master)
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="" runat="server" id="liHome">Home</a>
<a class="mdl-navigation__link" href="" runat="server" id="liDashboard">Dashboard</a>
</nav>
Now in your code behind for another page, for mine, it's the login page...
On PageLoad() define this:
HtmlAnchor lblMasterStatus = (HtmlAnchor)Master.FindControl("liHome");
lblMasterStatus.Visible =false;
HtmlAnchor lblMasterStatus1 = (HtmlAnchor)Master.FindControl("liDashboard");
lblMasterStatus1.Visible = false;
Now we have accessed the site masters controls, and have made them invisible on the login page.

How to Add Dynamic Meta Tag to Site.Master from Database?

We have a series of meta tag values in our database that need to be passed to the Site.master depending on the site being served. How would I include the BingMetaTag database field value in my Page Load event? We need to pass the content attribute value from the database to the meta tag in the master page.
Page_Load is as follows:
public partial class Site : System.Web.UI.MasterPage {
protected DealerInformation objDealerInformation = null;
protected DealerSite objDealerSite = null;
protected ConnectionStringConfig dbConfig = null;
protected void Page_Load(object sender, EventArgs e) {
dbConfig = Session["DBConfig" + Request.Url.Host] as ConnectionStringConfig;
objDealerInformation = CommonFunctions.GetDealerInformation(dbConfig);
objDealerSite = Session["DealerSite" + Request.Url.Host] as DealerSite;
try {
imgGoogleAdServices.Src = string.Format(#"//googleads.g.doubleclick.net/pagead/viewthroughconversion/{0}/?value=0&label={1}&guid=ON&script=0;", objDealerSite.GoogleConversionID, objDealerSite.GoogleConversionLabelRemarketing);
} catch {
imgGoogleAdServices.Src = "";
}
try {
WebEntitiesModel context = new WebEntitiesModel(dbConfig["WebConnection"]);
String aspPage = HttpContext.Current.Request.Url.AbsolutePath.ToString().ToLower();
MetaTag pageMetaTag = (from m in context.MetaTags
where m.Page == aspPage
select m).Single();
Page.Title = (pageMetaTag.PageTitle != null ? pageMetaTag.PageTitle : "");
Page.MetaKeywords = (pageMetaTag.MetaKeywords != null ? pageMetaTag.MetaKeywords : "");
Page.MetaDescription = (pageMetaTag.MetaDescription != null ? pageMetaTag.MetaDescription : "");
} catch {
Page.Title = "";
Page.MetaKeywords = "";
Page.MetaDescription = "";
}
Note that we are already bringing in title, meta keywords and meta description from another database table. We just need to add the value of BingMetaTag as a separate meta tag entry.
It sounds like you're using WebForms rather than MVC, which actually makes this a bit easier.
In your Site.master file:
<head>
<meta id="someMeta" runat="server" name="something" value="" />
</head>
In your Site.master.cs file's class:
protected HtmlGenericControl someMeta;
public String SomeMetaValue {
get { return this.someMeta.Attributes["value"]; }
set { this.someMeta.Attributes["value"] = value; }
}
In your page's class
public void Page_Load(Object sender, EventArgs e) {
SiteMaster master = (SiteMaster)this.Master;
master.SomeMetaValue = "someValueFromDatabase";
}
If you have multiple types of Master pages in your project then this code will fail, so add appropriate guards and checks as needed.

how to dynamically load the aspx page in div tag ?

Hi everybody...
I am trying to load the contents of one aspx page into div tag of another aspx page, i dont want to use jquery for it. can anybody please suggest me the server side solution to load the div tag dynamically on click of button.
Thanks in advance
Just get the page it self and send it to the control
in HTML file
<div class="code">
<pre><asp:Literal id="litCode" runat="server /></pre>
</div>
in CS file
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
populate();
}
private void populate()
{
litCode.Text = getSoureCodeFromFile("http://localhost:21300/Search.aspx");
}
private string getSoureCodeFromFile(string url)
{
string r = "";
using (WebClient wc = new WebClient())
{
r = wc.DownloadString(url);
}
return r;
}

Resources