To use hidden field value in another aspx file - c#

In the below code i have a hidden value in sample.ascx and i want to use that value in sample.aspx in its codebehind.pls help me to do this.
Sample.ascx
txthidd.Value = "Hai";
<asp:HiddenField ID="txthidd" runat="server" />

You can create a Public property in your ascx like this
public string txt
{
get
{
return this.txthidd.Value;
}
}
and can access this in aspx like this
string textOnAspx = UC_UserControl.txt;

In the codebehind, you should create a property getting the field:
public string TxtHidText{
get
{
return txthidd.Value;
}
}
Then, you'll reference it per the id, let's say you'll have something like this in ASPX:
<u1:Sample id="SomeSampleContentOfThePage" />
and in codebehind, it will be accessible via
var text = SomeSampleContentOfThePage.TxtHidText;
Note that if you want to set it from the other aspx, you should create a set part as well.

Related

How to set the value of a usercontrol's property in ASP.NET?

There are 3 dropdownlists that are being used at several places. So I decided to put them in a userControl.
<%# Control Language="VB" AutoEventWireup="false" CodeFile="DetailsDropDownList.ascx.vb"
Inherits="UserControls_DetailsDropDownList" %>
<asp:DropDownList ID="dedMoth" runat="server"></asp:DropDownList>
<asp:DropDownList ID="dedDay" runat="server"></asp:DropDownList>
<asp:DropDownList ID="dedYear" runat="server"></asp:DropDownList>
In the Code behind, I've defined a property so that I capture the value to display.
public DateTime DetailsDate { get; set; }
So I'm setting the property this way: myUserControl.DetailsDate = myObject.myDate
The problem is that no value is being set. When I set the breakpoint, I notice that UserControl's life cycle starts after that of the main page.
I've tried this as well but no luck.
<uc1:DetailsDropDownList runat="server" ID="myUserControl"
DetailsDate ="<%= myObject.myDate%>" />
So How do I set the value of a UserControl?
You need to persist your property. If you don't it's reset each and every single time the page loads, just like any other local variable. You can easily persist into viewstate though like so:
public DateTime DetailsDate
{
get{
if(ViewState["DetailsDate"] == null)
{
return DateTime.Today;
}
else
{
return (DateTime)ViewState["DetailsDate"];
}
}
set {ViewState["DetailsDate"] = value;}
}
Add Page.DataBind(); in Page_Load() event. This will bind page data. Once debugger pass through page load check expected date is coming or not.

How to get public property in ImageURL?

I have one public property named "ID"
public int ID { get; set; }
and an asp Image control
<asp:Image ID="ImagePicture" runat="server" />
I want to get the ID into a ImageURL. I tried like this
<asp:Image ID="ImagePicture" ImageUrl='ImageHandler.ashx?ID='<%= ID %> runat="server" />
but I get an Error: Server tags cannot contain <% ... %> constructs.
Does anyone know how I can fix it, or propose me another way to get the ID?
Thanks
It would be a lot cleaner to just do this in the code behind:
ImagePicture.ImageUrl = string.Format("ImageHandler.ashx?ID={0}", this.ID);
Did you try to set the value by the page_load method?
Something like that should work:
ImagePicture.imageURL = "yourLink.ashx?id=" + valueParam;

how can i pass the querystring into aspx

i have url like this : DossierSoin_Fiche.aspx?SoinId=1
how can i pass this SoinId into dx:ASPxPopupControl
<dx:ASPxPopupControl ID="ASPxPopupControl_Surveill"
ContentUrl="~/PopUp/Surveillance.aspx?SoinId=<%=SoinId %>" ?
thanks you in advance
PS : i can not use code behinde, because it will reload the page, then i will lost the data that have not save in database. I use callback instead, so i need pass this querystring value on aspx not in aspx.cs
In your containers codebehind:
protected string SoinId
{
get
{
return Request["SoinId"];
}
}
And use the code you have.
make a property "SoinID" (if you dont already have one)
protected string SoinId {get;set;}
(type of modifier is up to the OP, could also be public).
then, assign a value to the property in your page_load:
SoinId = Request.QueryString["SoinID"];
your .aspx code can stay the same if you use it like this.
Just pass the value to the public property in the CodeBehind of the page.
ASPxPopupControl_Surveill.ContentUrl = ...
[edit made with thanks to rs.]
Option A:
1) Declare a protected variable named SoinId in the scope of your aspx page.
2) In the Page_Load event, add this:
if(!Request.QueryString["SoinId"]==null)
{
SoinId = Request.QueryString["SoinId"];
}
Opetion B:
Replace your aspx code with this:
<dx:ASPxPopupControl ID="ASPxPopupControl_Surveill"
ContentUrl="~/PopUp/Surveillance.aspx?SoinId=<%=Request.QueryString["SoinId"] %>">
Edit: Consider using a property as other colleagues proposed. It's more ellegant.
I would imagine you could just assign the value in the code behind.
e.g.;
ASPxPopupControl_Surveill.ContentUrl = "~/PopUp/Surveillance.aspx?SoinId=" + Request["SoinId"].ToString();

Saving a value to and reading from the viewstate

I'm not too familiar with .NET, but I want to save a simple value (a number between 1 and 1000, which is the height of a particular div) to the viewstate and retrieve it when the update panel reloads (either in the markup somewhere or with javascript). What is the simplest way to do this?
This page gives me the following code:
string strColor;
if (Page.IsPostBack)
{
// Retrieve and display the property value.
strColor = (string)ViewState["color"];
Response.Write(strColor);
}
else
// Save the property value.
ViewState["color"] = "yellow";
However, I'm not totally clear on where or how to access the example strColor.
Since this is in the code behind, where will Response.Write even spit that code out? I couldn't find it when I tried this code. And how do I use javascript to set that value, instead of setting it in the code behind?
You can simply set the div as a server control as so:
<div id="yourdiv" runat="server" ...
And when the page posts back; simply set it's height by setting its attributes; for example:
yourDiv.Attributes("style","height:"+height_read_from_ViewState+"px;");
Or, you can store the height on the client side, using a Hidden field and reading that hidden field's value on the server side to set the div's height.
<asp:hiddenfield id="hdnHeight" runat="server" />
You set the height in Javascript as so:
function setHeight(value)
{
document.getElementById('<%=hdnHeight.ClientID').value=value;
}
And on post back on server side:
yourDiv.Attributes("style","height:"+hdnHeight.Value+"px;");
I would change strColor to a property and use the viewstate as a backing store for the propery.
public string strColor
{
get
{
return ViewState["strColor"];
}
set
{
ViewState["strColor"] = value;
}
}
And then you would use it like any other property:
if (Page.IsPostBack)
{
// Retrieve and display the property value.
Response.Write(strColor);
}
else
// Save the property value.
strColor = "yellow";

Pass a value from aspx to ascx

I have a web page and it contains an user control inside of it. I have a property on the aspx page which gets set in the pageinit method and I need to that propery on ascx page.
How can I get it?
Create a public property inside the ascx and set it at the same time you set in the aspx page.
Just to let you know, PreInit is a EventHandler not a method.
MyAdminPage myPageInstance = this.Parent as MyAdminPage;
if(myPageInstance != null)
{
...
}
There has been a few questions on this.
Reference .aspx property from .ascx
The easiest options are as follows:
Use a public variable and access it from the parent page.
Assign the variable to a hidden field on the ascx front end. A field like this: <asp:HiddenField ID="ascxField" runat="server" /> .
The example below is for #1, but #2 is almost the same.
Example #1:
Aspx page:
Front end:
<%# Register TagPrefix="Admin" TagName="MyUserControl" Src="~/UserControls/.../MyUserControl" %>
...
<Admin:MyUserControl ID="MyUserControl" AutoPostBack="true" runat="server" Visible ="false" />
Code Behind:
this.MyUserControl.Variable1 = 1;
this.MyUserControl.Variable2= "value";
Ascx page:
Code Behind
public int Variable1 { get; set; }
public string Variable2 { get; set; }

Categories