Asp.net ASP:Login and forms - asp.net

On my webpage, I use ASP:Login, when members login to my page, very standard.
I also have a contact-form.
When I use my contact-form, there is no problem, but when I log in as a member, at use the contact form, nothing gets submitted, but the "form page" just loads again.
If I then fill out the form again, and hit the submit-button, the information is send.
My form is build like this:
<asp:Panel id="NameSug" Runat="server" DefaultButton="SendSug">
<asp:Textbox name="NavnForslag" id="NavnForslag" runat="server" width="200" MaxLength="18" /><br/><br/>
<asp:imagebutton id="SendSug" onclick="CupName_click" runat="server" ImageUrl="images/login.gif"/>
</asp:Panel>
So if I'm not logged in, and fill out the form, it sends right away. If I'm logged in, I have to hit the "send" button, fill out the form again, and hit send again.
My backend-file looks like this:
public partial class usercontrols_msgSystem_CupNameSugFront : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// Do something
}
protected void CupName_click(object sender, ImageClickEventArgs e)
{
// Do something else
}
}
My overall form-tag (I have one, that "surrounds" all the forms), looks like this:
<form id="DasTeam" runat="server">
Also, my login-form looks like this:
<asp:Login id="Login1" runat="server" DestinationPageUrl="~/Login.aspx">
<LayoutTemplate>
<div id="login">
<asp:Panel ID="panelLogin" runat="server" DefaultButton="submitLoginBtn">
<p class="tekst_alias">Alias:</p>
<p class="box"><asp:TextBox ID="UserName" runat="server" Width="150px"></asp:TextBox></p>
<p class="tekst"> Password:</p>
<p class="box"><asp:TextBox ID="Password" runat="server" TextMode="Password" Width="150px"></asp:TextBox></p>
<p class="login"><asp:LinkButton ID="submitLoginBtn" CommandName="Login" runat="server" CssClass="loginbutton">Login</asp:LinkButton></p>
<p class="failure"><asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal></p>
</asp:Panel>

Related

Connecting Session from a repeater's item to another page doesn't work

I want to take the text in the text box from the particular item in the repeater that was clicked, and use it on the page ViewRecipe2.aspx.
Currently, when you click a button on one of the items, it returns back to the repeater's page, but the repeater does not appear, instead of moving to the page ViewRecipe2.aspx.
This is my repeater in aspx:
<asp:Repeater ID="RepeaterR" runat="server">
<ItemTemplate>
<div class="wrapper">
<table>
<div class="box">
<div class="property-card">
<div class="property-image">
<div class="property-image-title">
</div>
</div>
<div class="property-description">
<asp:Button CssClass="h5" runat="server" ID="Button1" OnClick="Button1_Click" Text=<%# Eval("recipeName")%> BackColor="Transparent" BorderColor="Transparent"/>
<p><%#Eval("avgRating") %> stars</p>
<asp:Image class="img" runat="server" src=<%#Eval("recipePic") %> />
<asp:TextBox ID="hiddenTB" runat="server" Text=<%# Eval("recipeName")%> Visible="false"></asp:TextBox>
</div>
</div>
</div>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
This is the code behind on c#:
protected void Button1_Click(object sender, EventArgs e)
{
RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
string VR = (item.FindControl("hiddenTB") as TextBox).Text;
if (VR!=null)
{
Session["selectedRecipe"] = VR;
Response.Redirect("ViewRecipe2.aspx");
}
}
This is ViewRecipe2.aspx:
<asp:TextBox ID="TextBoxP" runat="server"></asp:TextBox>
And the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string theRecipeName = (Session["selectedRecipe"]).ToString();
TextBoxP.Text = theRecipeName;
}
}
Well, text box or hidden field is "never" null.
but, you need quotes around that "text" setting of the hidden field.
<asp:TextBox ID="hiddenTB" runat="server"
Text='<%# Eval("recipeName")%>' Visible="false">
</asp:TextBox>
Also, keep in mind, that with visible=false, then the markup is NOT sent nor rendered client side. This means that client side js code can't use that text box, but server side code can just fine grab the textbox, and then the value as you have.
However, while you "should" have those single quotes?
it should still have worked.
I would for testing, remove the Visible="false", and then you can actually see + verify that the value is correct.

change iframe label from main asp.net page

So, basically I have a simple asp.net page "Default.aspx" that has a textbox called "txt"and button "adjust_btn" and an iFrame that is linked to "preview.aspx" which has "label1" in header.
Here is the code I have for Default.aspx:
<p>
<asp:Button ID="preview_btn" runat="server" Text="Preview" Width="110px" OnClick="preview_btn_Click" />
<asp:TextBox ID="txt" runat="server" Width="200px" Height="25px"></asp:TextBox>
</p>
Here's the code I have in "Preview.aspx":
<h1> <asp:Label ID="Label1" runat="server" Text="Label1"></asp:Label> </h1>
Here's the code i have for the button click:
protected void preview_btn_Click(object sender, EventArgs e)
{
iframe.Visible= true;
// here i want something that does this iframe.label1 = textbox1.Text;
// so the label1 is in iframe and the textbox1 is in the main page..
}
Appreciate your help, team!

Remove ID from URL in ASP.net

I want to remove the ID from the URL in ASP.net 2010.
I tried to search in the net but I didn't get a clear solution.
Can you help me please or send me links for a clear solution to understand and apply in my website?
I have a simple datalist in the page Architecture.aspx. this datalist get info from SqlDataSource1 (see code below).
When I press in the LinkButton (Image1), a new page (ViewProject) opens, in the URL I have (http://localhost:51550/Portfolio/ViewProject.aspx?ID=1). I dont want to show the ID:
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID"
DataSourceID="SqlDataSource1" RepeatColumns="4" RepeatDirection="Horizontal" style="margin-right: 0px"
EnableViewState="false">
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server" BorderStyle = "None" Height="250px" Width="200px">
<table id="Table1" runat="server" style="height: 50px">
<tr >
<td style="color: #000000; width: 75%; font-weight: lighter" align="center">
<asp:LinkButton Width="145px" Height ="200px" ID="Image1" runat="server" CommandName="ViewDetails" CommandArgument='<%#Eval("ID") %>' OnCommand = "Click_LinkButton1" >
<div class ="pic">
<asp:Image ID = "im1" runat="server" ImageUrl= '<%# Eval("mainPic")%>' Width="150px" Height ="200px" BorderWidth="0px" />
</div>
</asp:LinkButton>
</td>
</tr>
<tr >
<td style="width: 75%; font-weight: lighter; padding-left:19px">
<asp:Label ID="lbl2" runat="server" Text='<%#Eval("title") %>' ForeColor="Gray" Font-Names="Lucida Sans Unicode" Font-Size="12px"></asp:Label>
</td>
</tr>
</table>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:PortfolioConnectionString %>"
SelectCommand="SELECT [ID], [title], [mainPic] FROM [Project]">
</asp:SqlDataSource>
protected void Click_LinkButton1(object sender, CommandEventArgs e)
{
string url = "ViewProject.aspx?";
url += "ID=" + e.CommandArgument;
Response.Redirect(url);
}
You can use it as
protected void Click_LinkButton1(object sender, CommandEventArgs e)
{
string url = "ViewProject.aspx";
Session("ID") = e.CommandArgument;
Response.Redirect(url);
}
and can access it as on the page load event of another page
protected void Page_Load(object sender, System.EventArgs e)
{
String strID = Session("ID").ToString;
}
To learn more about sessions visit asp.net-tutorials.com/state/sessions/, MSDN Article ASP.NET Session State and How to: Read Values from Session State
haraman's implementation is not good. His implementation didn't check for missing "ID" in the session. That means if someone navigates directly to the page without having clicked on that link button, they're going to get a NullReferenceException. Why would they navigate directly there? Perhaps they bookmarked it. Or they refresh the page after their session expires. Or they share the link with a coworker.
Instead, you should pass the ID in the URL. There's nothing unprofessional about it. It's certainly preferable to the problems that not including it would cause. If you're concerned about the looks, you can make it look nicer. You don't have to utilize query string.
Query string example:
example.com/ViewProject.aspx?id=987
ASP.NET has a concept of routing, which you can utilize in your application for a clean URL. Using routing, your URL can look like this:
example.com/ViewProject/987
And with some additional tools, you can make it search engine/user friendly (Stack Overflow does this):
example.com/ViewProject/987/My-Fancy-Project

wizard and asyncfileupload in next step

In my ASP.NET .NET 3.5 I have custom control that has UpdatePanel inside.
In that update panel I Wizard control with 7 steps.
In second step I want to upload attachments using AsyncFileUpload.
In my scenario user can add multiple files and they will show inside grid, so after upload he can add comments to them.
Everything works file if I have AsyncFileUpload in Step that is visible at beginning, UploadedComplete event is fired correctly, but when I start from different step I can't get that Upload to work.
I was thinking about using iframe, but I would like to avoid it because I need to have 5 upload components in different Steps.
Is it possible to get that AsyncFileUpload to work in Wizard?
My code is standard, nothing magical at moment:
ToolkitScriptManager
UpdatePanel
-ContentTemplate
--Wizard
---WizardSteps
----WizardStep 1
----WizardStep 2
-----AsyncFileUpload
----WizardStep 3
----WizardStep 4
-----AsyncFileUpload 1
-----AsyncFileUpload 2
----WizardStep 5
And my simple event handler
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
var fileUpload = (AjaxControlToolkit.AsyncFileUpload)sender;
if (fileUpload.HasFile)
{
string strPath = path + e.FileName;
AsyncFileUpload1.SaveAs(strPath);
}
}
As I wrote before I get that event if I start from Step 2 (ActiveStepIndex=1).
Here is my ascx code:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="PWS_Test.ascx.cs" Inherits="kontrolki_PWS_Test" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Wizard ID="Wizard1" runat="server" ActiveStepIndex="0">
<WizardSteps>
<asp:WizardStep runat="server" title="Step 1">
<asp:Label ID="Label1" runat="server" Text="Questions"></asp:Label>
<br/>
<asp:CheckBox ID="CheckBox1" runat="server" Text="One"/>
<br/>
<asp:CheckBox ID="CheckBox2" runat="server" Text="Two"/>
</asp:WizardStep>
<asp:WizardStep runat="server" title="Step 2">
<asp:Label ID="Label2" runat="server" Text="Choose Your image"></asp:Label>
<br />
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" FailedValidation="False" OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
</ContentTemplate>
</asp:UpdatePanel>
And here is cs code:
using System;
public partial class kontrolki_PWS_Test : System.Web.UI.UserControl
{
protected const string path = #"c:\temp\";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
var fileUpload = (AjaxControlToolkit.AsyncFileUpload)sender;
if (fileUpload.HasFile)
{
string strPath = path + e.FileName;
AsyncFileUpload1.SaveAs(strPath);
}
}
}
This works when I start from step 2.
As a workaround I've created a hidden div in my control just before wizard:
<div style="display: none">
<asp:AsyncFileUpload ID="AsyncFileUpload2" runat="server" />
</div>
and now my upload is working, but I would like normal solution instead of that workaround.
Add the attribute: enctype="multipart/form-data" to your <form> tag.
<form id="form1" runat="server" enctype="multipart/form-data">
...
...
</form>

get info from one page and show it on other page

i m working on shopping cart site
i want to show complete information of a product contained in a div section. on an other page. i have may div that contained information.
goal is to get the info on a new page when user clicks some product's image.
Error:
Control 'MainContent_ImageButton1' of type 'ImageButton' must be placed inside a form tag with runat=server.
HTML:
<div id="physics" runat="server">
<h3>High School Physics</h3>
<%-- <img src="images/Book.jpg" /> --%>
<asp:ImageButton ID="ImageButton1"
src="images/Book.jpg" runat="server" onclick="ImageButton1_Click" />
<p> Book Description Goes Here
<br />
<asp:Label ID="Label1" runat="server" Text="Label">PKR 770/-</asp:Label>
<br />
<asp:TextBox ID="TextBox1" Text="1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add" />
</p>
</div>
Code :
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
physics.RenderControl(w);
Session["mySessionVar"] = sw.GetStringBuilder().ToString();
Response.Redirect("ShowLarge.aspx", true);
}
HTML: where i want to show this info
<div id="ShowInLargeView" runat="server">
</div>
Code:
protected void Page_Load(object sender, EventArgs e)
{
ShowInLargeView.InnerHtml = (String)Session["mySessionVar"];
}
i want to show the complete info of Div in an other page. i m getting an error. this scenario is about shopping cart
i need help
please help.
The error you are getting is related to the fact that you are trying to add an event handler
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
In order to add an event handler to an element on your page it will need to be encapsulated in a 'form' with the runat="server" attribute. this will result in the click event of the ImageButton ending up in your code-behind.
so your aspx would become something like :
<form runat="server">
<div id="physics" runat="server">
<h3>High School Physics</h3>
<%-- <img src="images/Book.jpg" /> --%>
<asp:ImageButton ID="ImageButton1"
src="images/Book.jpg" runat="server" onclick="ImageButton1_Click" />
<p> Book Description Goes Here
<br />
<asp:Label ID="Label1" runat="server" Text="Label">PKR 770/-</asp:Label>
<br />
<asp:TextBox ID="TextBox1" Text="1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add" />
</p>
</div>
</form>
In general it should suffice to encapsulate your entire page in a form tag, this way all events will be passed to your code-behind.

Resources