asp.net: Setting up multiple forms on a page - asp.net

I have a master page that contains a search box which is validated as a required field before the user can submit the search field.
The problem I'm having now is that one of my content pages has a DetailsView which won't let me edit a record, because the search box coming from the master page is blank.
The structure of the code is like this:
Master Page:
<form runat="server">
<asp:RequiredFieldValidator ID="SearchBoxRequiredFieldValidator"
runat="server" ControlToValidate="searchTextBox"
Display="None" ErrorMessage="Enter an employee's last name"></asp:RequiredFieldValidator>
<asp:TextBox ID="searchTextBox" autocomplete ="off" runat="server" Width="180px"></asp:TextBox>
<asp:Button ID="SearchButton" runat="server" Text="Employee Search"/>
<!--.....-->
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</form>
The "MainContent" placeholder is populated with a page with only a DetailsView. How should I change my code so that I can submit forms from my MainContent pages, but also allow the Master page's search feature to function properly?
I'm pretty new to asp.net forms, so any help is greatly appreciated!

Take a look at ValidationGroups. You can separate each logical form into different validation groups giving the effect of multiple discrete forms.

Related

POST method issue with master page

I'm trying to submit the below form where a master page is applied.
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
What is your name?
<asp:TextBox ID="YourName" runat="server" ClientIDMode="Static"></asp:TextBox>
</p>
<p>
What is your age?
<asp:TextBox ID="YourAge" runat="server" Columns="3" ClientIDMode="Static"></asp:TextBox>
</p>
<p>
What is your favorite color?<br />
<asp:RadioButtonList ID="FavoriteColor" runat="server" ClientIDMode="Static">
<asp:ListItem Selected="True">Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
</asp:RadioButtonList>
</p>
<p>
<asp:CheckBox ID="EatGreenEggsAndHam" runat="server" Checked="True" ClientIDMode="Static"
Text="Will You Eat Green Eggs and Ham?" TextAlign="Left" />
</p>
<p>
<asp:Button ID="SubmitButton" runat="server" Text="Submit Form"
PostBackUrl="~/About.aspx"
onclick="SubmitButton_Click" />
</p>
</asp:Content>
I'm trying to use POST method to get submitted data from the form.
in About.aspx page i'm using the below code
Response.Write(Request.Form["YourName"].ToString());
expected behavior is to print the name typed in textbox with the id "YourAge" but the submitted form element ids have changed like below
ctl00$MainContent$YourName
why does this happen? i need to get the value of the control with the id "YourName"
I want to remove this "ctl00$MainContent$" part when submitting form. retrieving the form values are out of my scope and it's done by some other people. They only require form values to be submitted as "YourName" not "ctl00$MainContent$YourName"
Try to utilise the abstraction that ASP.net provides. You can use YourName.Text to retrieve the value of the YourName TextBox.
Response.Write(YourName.Text);
Also, ctl00$MainContent$YourName is not the ID of the element generated, but its name. The ID will probably be something like BodyContent_YourName.
In the first place, it is not advisable to directly read from the Request.Form. Most of the time You should be using the YourName.Text to read the value.
But, if you would still like to read the value from Request.Form you need to understand how the ASP.NET pipe line and http protocol works. When you submit a page from browser, browser would create a HTTP Request with key value pairs, where the key denotes the "name" of the input control while the value denoted the corresponding value.
In your code you are only setting the ID to be static but not the name. So, to set the name same as ID, you can use the techniques described for this SO Question
Udpate
Another approach I could think of is as below
Response.Write(Request.Form[YourName.UniqueID].ToString());
The Unique ID is the name which is rendered to the element and you can use the same for fetching the values from the Request.Form

Textbox losing data within HeaderTemplate on Postback in ASP.Net

I have a ASP.Net GridView and I build the column collection myself. Within the column collection I have a HeaderTemplate and within there I have a textbox which I use to filter the records in the grid.
When I enter text within this textbox and perform an action on the grid which causes a postback (i.e. changing the page) I lose the text within my textbox.
Anybody got any ideas as to why this data is lost?
My ASP code for the header template is below:
<HeaderTemplate>
<asp:Label ID="Label1" Text="Number" runat="server" />
<asp:TextBox ID="textBoxNumberFilter" runat="server" />
<asp:ImageButton ID="buttonFilterNumber" runat="server" OnClick="buttonFilters_Click" />
</HeaderTemplate>
Thanks in advance. I'm using ASP.Net 4.0
I think this might help.
http://www.codeproject.com/Articles/38714/How-To-Perpetuate-Dynamic-Controls-Between-Page-Vi

Create a web page as 2 parts

I want to develop a registration form with 2 panels. One panel is personal information and another is address details. In these panels the user fills in all details of personal information and after completion of this, the user clicks on Add Address Details. If the user clicks on that, the second panel should be visible without page refresh. How can I accomplish this?
you would use javascript to append the append the second form to to the div of the first form. Or however you have it setup. So in jquery it would look something like this:
<script type="text/javascript">
$('#buttonid').click(function() {
$('#divid').append("<form><input />etc etc etc</form>");
});
</script>
You should use the ASP.NET Wizard control. It is the perfect tool for these scenarios. Scott Gu has a post with some links explaining how to use it. I recommend you look at it.There's a video linked that walks you through a full example.
I don't know, but hiding/showing panels based on certain conditions on the same page feels inelegant and hacky to me.
You should look into using an UpdatePanel. Within there you could place to Panels with different form information. Tie in a few button clicks and hide/show the different panels.
Edit with a simple example
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="upnl" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Panel ID="pnl1" runat="server">
<!--personal info-->
<asp:Button ID="btn1" runat="server" Text="this button could validate personal info, hide pnl1 and show pnl2" />
</asp:Panel>
<asp:Panel ID="pn2" runat="server" Visible="false">
<!--address info-->
<asp:Button id="btn2" runat="server" Text="this button could validate address information and finally submit the form." />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

Handle Enter Key for search and login page

I have an ASP.Net 3.5 in VB.Net page that uses a master-page and has several pages it uses to display content. On the master-page their is a search text-box for the site so that shows up on all the pages. Their is also a login page screen to get to a members account.
The issue is: When you are on a content page I would like when a user puts something in to search for and presses enter it will process the search request.
But....
If you are on the login page I would like to disable enter key for the search and if the user presses enter on the login page to process the login request.
Is their an easy way to accomplish this?
Create your form in an asp:panel and set the DefaultButton to be the button of the form you want to submit when enter is pressed
<asp:Panel DefaultButton="btnSubmit" runat="server">
<asp:TextBox ID="UserName" runat="server"/>
<asp:TextBox ID="Password" TextMode="Password" runat="server"/>
<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Sign In" runat="server"/>
</asp:Panel>
Note: asp:panel creates a div

UpdatePanel with GridView with LinkButton with Image Causes Full Postback

So this might be a fairly specific issue but I figured I'd post it since I spent hours struggling with it before I was able to determine the cause.
<asp:GridView ID="gvAttachments" DataKeyNames="UploadedID" AutoGenerateColumns="false" OnSelectedIndexChanged="gvAttachments_SelectedIndexChanged" runat="server">
<EmptyDataTemplate>There are no attachments associated to this email template.</EmptyDataTemplate>
<Columns>
<asp:TemplateField ItemStyle-Width="100%">
<ItemTemplate>
<asp:LinkButton CommandName="Select" runat="server"><img src="/images/icons/trashcan.png" style="border: none;" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In the ItemTemplate of the TemplateField of the GridView I have a LinkButton with an image inside of it. Normally I do this when I have an image with some text next to it but this time, for whatever reason, I just have the image. This causes the UpdatePanel to always do a full postback.
Instead of changing the markup, you can goto web.config and specify ClientIDMode="Auto" in the pages tag.
Reason why UpdatePanel behaving like this is because the ClientIDMode is getting generated will be too long for UpdatePanel to register. So the ClientID got truncated in middle and such control will be treated like unregistered Control.
For more information read the following:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
Change the LinkButton to be an ImageButton and the problem is solved.
<asp:ImageButton ImageUrl="/images/icons/trashcan.png" Style="border: none;" CommandName="Select" runat="server" />
Above solutions also work, But There is one more thing to check. Check form tag for your page. If id attribute is missing, you will get same issue.
If you form tag is as given below (without id), you will get issue:
<form runat="server">
<!-- your page markup -->
</form>
Please add id, as given below:
<form id="form1" runat="server">
<!-- your page markup -->
</form>
You will not need to update ClientIDMode in web.config or page or control.
You will not need to change your linkbutton in markup.
You will not need to register control for asynch postback from code behind.

Resources