Another way to pass a value across WebForms - asp.net

If I use a Server.Transfer command with a true value passed for the "PreserveForm" 2nd param's value:
Default.aspx:
Server.Transfer("WebForm1.aspx", True)
...I can access the value of the textbox "TextBox1" control on the first page, Default.aspx, from "WebForm1.aspx" as follows:
lblPassedValue.Text = Request.Form("ctl00$MainContent$TextBox1")
I know that I can control the way that IDs are generated in Visual Studio 2010, but how about controlling the NAME property?
I would like it to read:
lblPassedValue.Text = Request.Form("TextBox1")
I'd also rather use ASP.NET Textbox controls rather than a native HTML textbox.

Server.Transfer("Page.aspx", True)
On Page.aspx:
<%# PreviousPageType VirtualPath="~/SenderPage.aspx" %>
Dim Test = CType(Me.PreviousPage.FindControl("TextBox1"), TextBox).Text

Related

Variable is not being replaced in ASPX

I have 2 ASPX files:
In A.aspx:
<%
string user = "in A.aspx.cs";
Response.Write(user);
%>
In B.aspx.cs
string id = Request.QueryString["id"].ToString();
​Response.Write(id);
When I run A.aspx and click on the link, the value of id from B.aspx is "<%=dz_now%>".
Why?
The problem is that <%=variable %> does not work within server side code. You need to add the strings together like this:
string user = "in A.aspx.cs";

Transfer between Page and Web control?

There are test.aspx page and test.ascx web user control.
I have a button in test.aspx = btn_test and above code in my button is :
Dim ct As Control = Page.LoadControl("test.ascx")
Panel1.Controls.Add(ct)
There is a dropdownlist with value 1 to 10 in test.aspx and there is label_test in test.ascx
I need some code when test.ascx loading, get dropdownlist.selectedvalue and show it in label_test.
Please help me !
There are a number of ways to implement this. One you could try would be to cast the test.ascx web control being loaded like so (replace TestControl with the class name for the control):
Dim ct As TestControl = CType(Page.LoadControl("test.ascx"), TestControl)
And then create a public property in the control which you would use to set the value from the DropDownList.
Dim ct As TestControl = CType(Page.LoadControl("test.ascx"), TestControl)
ct.DropDownListValue = DropDownList.SelectedItem.Value
Panel1.Controls.Add(ct)
This property would then be used to set the labels value (either directly using the set accessor or via a method within the test.ascx control).

Need to reference controls/properties in an ascx control (asp.net, written in vb)

I have a user control that I'm adding to a webpage dynamically. The ascx has a couple of controls that I want to have access to at runtime. I can access the ascx itself, but none of the controls on the ascx are available. I have tried adding a simple public variable and also tried adding a public property to the ascx, but I am unable to get access to either of them at design time (compile errors). I would appreciate any ideas - I'm stuck... :-)
I added the following to the code-behind of the ascx control:
Public Property areaCode() As String
Get
Return iebEmpPhoneAreacode.Text
End Get
Set(ByVal value As String)
iebEmpPhoneAreacode.Text = value
End Set
End Property
Public AreaCodeStr As String = ""
and am trying to use variations of the following to access the property/ascx controls:
For Each ctrl As Control In pnlPhones.Controls
If ((TypeOf ctrl Is ctrlPhone) And (ctrl.ID = vbNullString)) Then
(DirectCast(ctrl, ctrlPhone)).AreaCodeStr = "test"
'or try this
ctrl.areaCode = "test"
End If
Next
The hosting page should have an #Reference Directive pointing to the loaded ascx so it will be compiled with the page.
Something like:
<%# Reference VirutalPath="YourReferenceControl.ascx" %>
This should go in the directives area somewhere below the #Page directive.
http://msdn.microsoft.com/en-us/library/w70c655a.aspx

previouspage.findcontrol getting variable from previous page

i am trying to retain the value of a variable from a previous page. i believe this is the C# way to do this, but i would like the vb.net way can someone please help:
TextBox myTxt = (TextBox)Page.PreviousPage.FindControl("previousPageTextBox");
currentPageTextBox.text = myTxt.Text;
i would like to know how to code this in vb.net
i tried this:
Dim myTxt As TextBox = CType(Page.PreviousPage.FindControl("Textbox1"), TextBox)
TextBox1.Text = myTxt.Text
but i got this error msg:
use the new keyword to create an object instance
This should do it, also make sure you are using Server.Transfer to go between pages.. but I am sure you already know this:
Dim myTxt as TextBox = CType(Page.PreviousPage.FindControl("previousPageTextBox"), TextBox)
currentPageTextBox.text = myTxt.Text
dim txt as TextBox =CType( Page.PreviousPage.FindControl("previousPageTextBox"),
TextBox)
currentPageTextBox.Text = txt.Text
PS:- You need to set the previous page in the page where you want to find the control.
<%# PreviousPageType VirtualPath="~/Test.aspx" %>
Also better way will be to create an function on previous page which returns the text in textbox.
internal function TextBoxText() as string
return myTextPage.Text
end function
and use this on next page like this:
currentPageTextbox.Text = Page.PrevousPage.TextBoxText
let me know if this works because I've not used vb for long long time.
PPS:- It is only available if you user Server.Transfer or go to currentPage using some asp.net contorl like LinkButton from simple href links PreviousPage is null

Problem while Creating Dynamic ModalPopup

i want to create a modalpopup dynamically but i come across a problem.I pasted my sub here and i dont know what to do for that problem.When i want to show modalpopup,it says
"Control 'mdldelete2' of type 'ModalPopupExtender' must be placed inside a form tag with runat=server." How can i solve this?
Public Sub Raise_Alarm(ByRef p_Page As Page,
ByRef p_AssignedButton As System.Web.UI.WebControls.Button,
ByVal p_Message As String)
Dim mdldelete2 As Global.AjaxControlToolkit.ModalPopupExtender =
p_Page.Page.FindControl("mdldelete2")
If mdldelete2 Is Nothing Then
mdldelete2 = New Global.AjaxControlToolkit.ModalPopupExtender
End If
With mdldelete2
.TargetControlID = p_AssignedButton.ID
.PopupControlID = "pnlDelete"
.ID = "mdldelete2"
.BackgroundCssClass = "modalBackground"
.OkControlID = "btnDeleteOk"
.CancelControlID = "btnDeleteCancel"
End With
p_Page.Controls.Add(mdldelete2)
Dim mylabel As Label
mylabel = p_Page.FindControl("lblStatus")
mylabel.Text = p_Message
mdldelete2.Show()
End Sub
Really, you should be adding the mdldelete2 control to the Controls collection of the Form control, rather than the Page directly - that might help.
I often find it's easier to add a PlaceHolder control to the page for this sort of thing - it doesn't add anything directly to the page, but gives you a named container to find and add controls to.
Also, just a point - if you did find an instance of the control with the Page.FindControl method, then you don't need to add it to a form collection again, as it's already in there.
Looks like you need to add a ScriptManager control to the aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

Resources