Handling FormView databinding event on MasterPage from ContentPage - master-pages

Hoping this is simple. My goal is to have my masterpage carry a formview which can bind data retrieved based on variable held in my content page, or in the alternative based on variable held as a session variable. As a test, I set up a masterpage including a formview id=testerFV. There is also a label on page id=receiveno. From the masterpage I made a public property:
Public ReadOnly Property testerFVx() As FormView
Get
Return Me.testerFV
End Get
End Property
Similarly, a public property for the receiveno label.
I added <%# MasterType VirtualPath="~/masterpages/testmaster.master" %> to my content page. On my content page, I included a button titled testit. Backcode for the testit button is this:
Protected Sub testit_Click(sender As Object, e As System.EventArgs) Handles testit.Click
Dim tester1FVx As FormView = Master.testerFVx
If tester1FVx IsNot Nothing Then
Master.receivenox.Text = "testerfvx is not nothing"
Else
Master.receivenox.Text = "testerfvx is nothing"
End If
End Sub
The label displays "testerfvx is nothing" so apparently the receiveno label is findable in the masterpage through the public property, but I'm not finding the formview.
In another quick test, I tried binding the formview on the masterpage on the page load with a dummy collection like this:
Protected Sub page_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim thisthing As New List(Of fakenumbers)
Dim thisone As New fakenumbers
thisone.Thisnumber = 42
thisthing.Add(thisone)
testerFV.DataSource = thisthing
testerFV.DataBind()
End Sub
But I get an object not found error.
So: bottom line, how do I access a formview located on a masterpage from an event happening on my content page and using variables currently held on the content page. Also, if I include a testerFV_databound handler in the master page, will it respond correctly when I've triggered the databinding event from my content page?
Any help would be appreciated. Thanks!

Related

how to use server side button click event in mvc4?

I have to make a textbox somewhere in the page and a button using ASPX View Engine.That page already contains partial views. User will enter his data (code) on textbox and at button click, server code will manipulate that data and show an alert or message that data is processed. How to do that in mvc4?
I have found that we can write server script in page like
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs)
ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('Message Sent');", True)
End Sub
Private Sub DataProcess(ByVal Data As String)
'Code to check data
End Sub
</script>
You can create a separate Ajax Form ccontaining only this button and the textbox. So you will call an Action in a certain controller then this Action will return a string or a Json. And via an UpdateTargetId property you can display a dialog box. Giving you the code is difficult because I don't know the structure of your code. You can aslo write the Ajax call using Jquery.
This link can help you

How can I handle a submitted form?

I have a strong typed view from a model class. I would like to add there a text field where I can pass a number. This number should told the controller, how often the object should be added to the database. But in the View I only have the submit button and my controller function handles this postback
' POST: /Adminpanel/AddHardware '
<Authorize()>
<HttpPost>
Function AddHardware(ByVal hw As Hardware) As ActionResult
If ModelState.IsValid Then
db.Hardware.Add(hw)
db.SaveChanges()
Response.Redirect("~/Adminpanel/Hardware")
Else
Response.Redirect("~/Adminpanel/Hardware")
End If
End Function
it should be look like:
' POST: /Adminpanel/AddHardware '
<Authorize()>
<HttpPost>
Function AddHardware(ByVal hw As Hardware, ByVal amount As Integer) As ActionResult
If ModelState.IsValid Then
For i As Integer = 0 To amount
db.Hardware.Add(hw)
db.SaveChanges()
Next
Response.Redirect("~/Adminpanel/Hardware")
Else
Response.Redirect("~/Adminpanel/Hardware")
End If
End Function
How can I add this text field in my view and pass the value to the controller?
Add a input element in your view (inside your form) and give it name="amount". And when the form is submitted MVC will automatically bind the value from view to your controller. You can access the value of amount in controller directly.
NOTE: The binding depends on the name of the element. If your input tag has the name amount and in your controller parameters if you have a parameter amount MVC will bind the value to this parameter. So always make sure your element name and the parameter name are the same.
You should be able to simply add an <input ... /> to your form with an id of amount, and MVC will take care of binding the value to your amount parameter.

passing values using viewstate across postback in multiview

I have a multiview and have 2 views inside it. I am going to paste a sample code.
<asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
<asp:View ID="View1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="view1" />
<asp:Label ID="Label2" runat="server" ></asp:Label>
</asp:View>
<asp:View ID="View2" runat="server">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="view2" />
</asp:View>
</asp:MultiView>
I want the value in txtbox1 to be there in postback. Though multiviews maintain state i do response.redirect to pass querystring to view2. Since i do postback i cannot use the value in txtbox1(in view1), in view2. The value in txtbox1 becomes null during postback. I tried the following code
Public Partial Class viewstatetest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack()) Then
MultiView1.ActiveViewIndex = 0
Else
TypedPassword = TextBox1.Text
TextBox1.Attributes.Add("value", TypedPassword)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
MultiView1.ActiveViewIndex = 1
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
MultiView1.ActiveViewIndex = 0
Label1.Text = TextBox1.Text
Response.Redirect("viewstatetest.aspx")
End Sub
Public Property TypedPassword() As String
Get
If (ViewState("TypedPassword") IsNot Nothing) Then
Return CStr(ViewState("TypedPassword"))
End If
Return ""
End Get
Set(ByVal value As String)
ViewState("TypedPassword") = value
End Set
End Property
End Class
When the page loads for the first time, i type something in txtbox1 in view1 and click on the button, view2 is loaded and i have a code that gets the value of the txtbox1 and writes the value inlabel1 in view1. And when i do response.redirect the textbox1 becomes null and view also become null.
Why there is no value in viewstate?
Thanks!
Asp.Net viewstate is very different from normal get/post browser requests in other systems such as Rails, PHP, or even Asp.Net MVC.
Here is what is going on in your scenario:
User's browser does HTTP get on page for first time. This is not a postback.
User fills in TextBox1 and clicks Button1. This is a postback (HTTP post).
On the server, the information in the encrypted hidden __VIEWSTATE variable is unpacked and compared with the new values for TextBox1 and Button1 according to the post data in the request.
Asp.Net identifies state changes and fires events in your server code. This results in the change of the active view index to 1.
The browser now receives the page with View2 shown.
User hits Button2. This is a postback (HTTP post).
The server goes through the same process as before comparing viewstate to post data and fires the Button2 click event.
Your code now does something different. When you do a Response.Redirect, the server does not send back the page with new viewstate. You are sending a short header without the preserved viewstate information in the hidden form variable. The redirect forces the browser to immediately do an HTTP get operation to the url specified. THIS IS NOT A POSTBACK and the view state has been lost. It is exactly the same scenario as the first HTTP get from the user's browser in step 1.
I hope this helps. I think it is safe to admit that Microsoft has recognized the drawbacks from their viewstate model in Asp.net. It makes it quite difficult to implement sexy, modern ajax applications where the browser is maintaining most of the application state and just wants to make small requests for new data from the server. I think this is one of the primary motivations for abandoning the viewstate model in Asp.Net MVC.

Pass model from strongly typed view back to a controller action - mvc2 VB.net

Here's what I have. I have an MVC application where all the data is tied together by VisitDate table in my database. The home page is a strongly typed view of type VisitDate, all it does is pull up some simple data. Now, here's where I'm having a problemo. I need a link that passes the current model in the view back to a seperate controller action so I can render a different page with different data.
Here are my two controller actions. I'm going from News.aspx to FrontPage.aspx and hopefully passing SchoolVisit.
Function News(ByVal SchoolVisit As SchoolVisitDate) As ActionResult
Dim db As New NewsData.NewsDB
Dim repos As New NewsRepository
Dim _classId As Integer
_classId = (From a In db.SchoolClasses Where a.VisitDateID = SchoolVisit.VisitDateID Select a.ClassID).Single()
ViewData("VisitDate") = FormatDateTime(SchoolVisit.VisitDate, vbShortDate)
ViewData("Staff") = repos.GetStaff(_classId)
ViewData("StockArticles") = From a In db.StockArticles Select a
ViewData("Articles") = repos.GetArticles(_classId)
Return View()
End Function
Function FrontPage(ByVal SchoolVisit As SchoolVisitDate) As ActionResult
Dim repos As New NewsRepository
Dim _VisitDateID As Integer
_VisitDateID = SchoolVisit.VisitDateID
ViewData("Editorial") = repos.GetEditorial(_VisitDateID)
Return View()
End Function
Html.ActionLink can be of help
Here's what you have to do:
You should make the News view strongly typed for SchoolVisitDate.
Have a form that submits the strongly typed SchoolVisitDate stuff post back to controller
Add a Post method to your controller
Have that post method redirect to the FrontPage view.

Dynamically changing Master Template in ASP.NET MVC

I have the requirement to support different Master pages on my application (ASP.NET MVC).
What is the recommended way to:
Pass the master page name to the view from.
Store the master page (in session, or something) so it sticks during a user's visit.
Use a custom base controller and inherit from it instead:
Public Class CustomBaseController
Inherits System.Web.Mvc.Controller
Protected Overrides Function View(ByVal viewName As String, ByVal masterName As String, ByVal model As Object) As System.Web.Mvc.ViewResult
Return MyBase.View(viewName, Session("MasterPage"), model)
End Function
End Class
I set my Session variable in the global.asax Session_Start:
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
//programming to figure out your session
Session("MasterPage")="MyMasterPage"
End Sub
you could throw the master page name into the session, but sessions are unreliable. i'd recommend throwing it in a db instead.
once you're in the page, you can change/set the master page by accessing page.masterpagefile. it's a string; just pass the .master name in.
Why not keep the Master Page on the user profile?
Then just change it on the PreLoad event.
http://www.odetocode.com/articles/440.aspx

Resources