Category Archives: Razor

[RESOLVED] Multiple User Controls Postback issue

Hello,

     I have built a couple of user controls and tested them individually.  Both controls are working as intended when they are ran individually on a test page.

     One control is designed to list records in a grid and has a textbox and search button.  By entering a value and hitting the search button, it makes another call to the database and returns back a refined result.

      Another control, will be used to upload various types of files to a location.  I have validation checking that a path was entered and that a description was entered for a textbox.

      Ok, the problem now is that when I have both controls on the same page, they affect each other.  So, if I do a filter search with the control that has a gridview, it works but the validation in the other control fire and come back with errors.

      Does validation fire any time a postback occurs?  I'm only checking for validation in the control when a person actually hits save.  Yet it still shows up. 

      Anyway, thanks in advance for any tips or advice.

 

 

 

Yes pretty much, the easiest way to 'group' validation on a specific postback is by use of the validationgroup attribute.

 

Please check this for more info - but it is incredibly simple to implement

 

http://msdn.microsoft.com/en-us/library/ms227424.aspx

Thanks Razor.  It worked great and was easy to implement. 

 

[RESOLVED] How to update a cookie's expiration date

Hi;

I'm using WP using Razor and set up a cookie in a page like this:

 

HttpCookie cookie = new HttpCookie("mycookiename");
cookie.Expires = DateTime.Now.AddMinutes(1);
cookie.Path = "/";
cookie.Values["sessionId"] = data;
cookie.Values["email"] = "email@domain.tld";
Response.Cookies.Add(cookie);

Just after that I do a redirect to another page which use a page layout. All other pages depends on that same page layout.
In that page layout I want to update the cookie's expiration time, to keep the cookie alive as long as the user requests any page (besides the logout and default pages of course) in the site. I use this to update the cookie in the page layout:

HttpCookie cookie = Request.Cookies["bvdepqas"];
if (!cookie.Value.IsEmpty())
{
	// Update the cookie expiration
	cookie.Expires = DateTime.Now.AddMinutes(Convert.ToInt32(1);
	Request.Cookies.Set(cookie);
}
else
{
	//Delete the cookie
	Request.Cookies.Remove("mycookiename");
}

But my cookie expires after 1 minute, even if I request pages that use the page layout before the expiration minute. Why isn't the cookie's expiration being updated?

 

Thanks!

 

Hi,

 

You are updating cookies using Request object. You need to update using Response object as:

HttpCookie cookie = Request.Cookies["bvdepqas"];
if (!cookie.Value.IsEmpty())
{
        // Update the cookie expiration
        cookie.Expires = DateTime.Now.AddMinutes(Convert.ToInt32(1);
        Response.Cookies.Set(cookie);//Request.Cookies.Set(cookie);

}
else
{
        //Delete the cookie
        Response.Cookies.Remove("mycookiename");//Request.Cookies.Remove("mycookiename");


}

In order to update cookies in browser use Response object.

Check my blog on how to work with cookies.

Thank you, works :)

@Scal, Would you mind marking the correct answer. This helps others to easily find the answer and motivates user to answer more queries.

Sure thing :)

[RESOLVED] Email subscription link

Hi Guys,

I'm getting started on a new project and would like to have visitors be able to enter their email address to subscribe to a newletter, via a link such as 'sign up' or 'join up'.

Is there anyone who has implemented this before in Webmatrix that would know how to go about it?

I'm not sure whether I need to use a helper or just go for the traditional php/cgi script.

Thanks

Chris

basically you just need to have the textbox and the button.

once the user fill in the textbox with their email address, then the button will act as the INSERT command for inserting the value to your DB.

To do this in webmatrix :

@{

 var db = Database.Open("YOURDATABASE");


if (IsPost) {
        var email = Request["yourEmail"];
        if (email.IsEmpty()) {
            ModelState.AddError("yourEmail", "You must specify an email address.");
        }

if(ModelState.IsValid) {
                    var insertQuery = "INSERT INTO YOURDATABASE (email) VALUES (@0)";
                    db.Execute(insertQuery, yourEmail );
         
                }

}



<label for="yourEmail">Your Email Address</label>
                <input type="text" id="yourEmail" name="yourEmail"/>
                @Html.ValidationMessage("yourEmail")

 <p class="actions">
            <input type="submit" value="Submit"/>
        </p>

Thanks for this, it took me a little while to get a rudimentary version going as I haven't really been using the program regularly recently so I was still trying to use the outdated code such as 'open.Database.File' etc.

Now just need to work out how to display the confirmation message correctly and this should be ready to roll.

Thanks again.

 

thats the beauty of razor syntax...simplify thing...you might need to do some edit to the code as i didnt try it out.., :)

you can place the confirmation message like the following...


@{ if(ModelState.IsValid) { var insertQuery = "INSERT INTO YOURDATABASE (email) VALUES (@0)"; db.Execute(insertQuery, yourEmail ); } 
 if (IsPost)
 { var body = "Thank you, we have received your email"; }
 }  
@body 

Fantastic, that's saved me a whole heap of time.

Thanks again..

Smile

btw, if is regarding WebMatrix, you can post to here at  : http://forums.asp.net/1224.aspx. The current place you post in for WEB FORMS :D

Thanks. didn't realise there was a separate WeMatirix forum...

Smile

 

[RESOLVED] asp:Menu hover failures in IE 8 Beta (and Google Chrome)

hoverover does not display sub menu items in ie8. navigation is now impossible. it works in compatibility mode but not normal mode.

Is web:menu a legacy standard now?

does web:menu need updating for IE8 and Chrome?

is there a workaround?

Ideas welcome please!

 

The trouble with the Menu control is that the HTML it renders is, well, less than perfect. I always recommend using the CSS Friendly Control Adapters (http://www.asp.net/cssadapters/), for which the menu works fine in Chrome. I haven't tried it in IE8, but it works in FF/Opera/Safari, so should be OK in IE8.

 

Thanks Dave.

Nice idea and a step forward but sadly even the cssadapters (which do indeed work in Chrome) do NOT work in IE 8.

Goto http://www.asp.net/cssadapters/ from IE8 and click on their own Examples or Help main menu items and you will see a neat little empty box surround but with no menu list rendered inside it.

The most basic MS example ASP:MENU code shows the problem with IE 8.

http://quickstarts.asp.net/QuickStartv20/aspnet/samples/ctrlref/navigation/Menu/StaticMenuItemCollection_vb.aspx 

Back to the drawing board...

Paul J. Weighell

PS The MS example code, and indeed my own, passes the WC3 html validation OK so the HTML cannot be quite that horrendous?

 

 

 

  

Will I supopse that was asking for too much. The holy war of IE8 defaulting to IE7 mode or true standards mode has raised it's ugly head, so until we get a decision they're willing to stick with, we won't know for sure whether the menu will work out of the box.

If they move toward true standards, and one feels they must as everyone else seems to be, then one can hope that they release updated and compliant versions of their own software where it currently fails to comply. That may however be wishful thinking :-) Thanks again for your time on this.

 

If only it were that simple. Read http://www.joelonsoftware.com/items/2008/03/17.html for a good analysis.

Backward compatibility rears it head on the rendering too; the ASP.NET team have know about rendering issues, but they can't change the default behaviour with potentially breaking existing applications. The adapters were a way around that, but they do have longer term plans for improving this situation.

Ta for the link - he gives a good explanation.

I have every sympathy for both sides of this particular razor blade journey. Having been in the industry since 1969 I have seen every ‘standard’ very soon overlapped by the next one. I know young developers who worship ‘open standards’ that they really believe are a universal goal but I have heard it all before and will only believe it 5 years after it happens.

Most developers know that with IE on 75% of machines it is the defacto standard of the moment for customers, and customers are the determinants not the idealists. So, when Redmond decide to adapt a new standard (ideal or otherwise) it will dominate and if that straddles an uncomfortable reality then we probably just have to add yet another layer of software abstraction

I am not holding my breath waiting and will develop a new menu for my stuff that will hopefully work reasonably well at least under the 5 likely major ‘standards’ of IE7, IE8, FireFox, Mozilla and Chrome !

Thanks again for your help.

 

 

try this out

<head>
  <!-- Use IE7 mode -->
  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  <title>My Web Page</title>
</head>

Thanks for that idea but of course it switches IE8 into 'legacy' mode and does nothing in Chrome, both states I wish to avoid.

The current workaround is to use asp:treeview instead of asp:menu. Treeview does work although it only has vertical mode so involves page rearrangment if one uses horizontal menu mode.

The longer term solution is to move away from asp:menu and create something that works across major browsers.

Paul J. Weighell

 

 

 

This is bug in IE8 as its beta, there may be othere, adn this solustion they are given on their site. so for now we have to do this pathc or switch the menu by using othere control. for Chrome there are lots many things which are not working right now, but they are still under development phase, they will release the stable version soon.

"for now we have to do this pathc or switch the menu by using othere control"

correct and instead of waiting for google/ms the least risk and fastest solution path is to switch controls.

thank you all for your help. this post is now resolved.

Paul J. Weighell

 

 

 try

http://code.msdn.microsoft.com/KB962351/Release/ProjectReleases.aspx?ReleaseId=2294

this patch for to fix IE8 prob.

asp:Menu control not rendering correctly in google chrome can be fixed.

  1. Create the App_Browsers
  2. right click on it and create a .browser file using any name you like (chrome.browser)
  3. put the below code in it

<browser refID="safari1plus">
      <controlAdapters>
        <adapter controlType="System.Web.UI.WebControls.Menu" adapterType=""></adapter>
      </controlAdapters>

</browser>

[RESOLVED] Difference between Web Fom and Web Page

Hi,

 What is the difference between Web Fom and Web Page?

Thanks

It is the same! The only difference is that Web Forms is an official term used by Microsoft.

Hope this helps

Both are same thing.

I want to know , whether you found somewhere that they are different ?

A Web Form is a subset of the category web page.  Web pages don't have to be .aspx pages, don't have to have a <form> on them, and don't have to have anything that is processed by the .Net framework.  But if you are talkinga bout .aspx web pages, a Web Form is the basic start for one.

Hi,

himchr

Web Fom and Web Page?

Both are different but build on top of the ASP.NET core framework (just like MVC and Dynamic Data).

ASP.NET Web Pages and the new Razor syntax provide a fast, approachable, and lightweight way to combine server code with HTML to create dynamic web content. Connect to databases, add video, link to social networking sites, and include many more features that let you create beautiful sites using the latest web standards.

 

ASP.NET Web Forms lets you build dynamic websites using a familiar drag-and-drop, event-driven model. A design surface and hundreds of controls and components let you rapidly build sophisticated, powerful UI-driven sites with data access.

I suggest you read further on these pages:

http://www.asp.net/web-pages

http://www.asp.net/web-forms

Grz, Kris.

Hi,

Thanks to all of you.

@XIII Can you explain difference between web page and web form, rather than giving definition of web page and web form? 

Thanks

You need to understand what they are before you can understand the difference. Assuming that this is not course work, and you are not looking for a simple definition to copy and paste without understanding it, you need to go through the links that XIII provided and do some research. The differences will be obvious to you if you do that.

Hi,

himchr

@XIII Can you explain difference between web page and web form, rather than giving definition of web page and web form? 

If you simply try to read both explanations it becomes obviously clear. Just remember that both have the same common ground but that the implementation's different and their purpose too.

Grz, Kris.