I have created a registration wizard in ASP.net that uses the MultiView control and I have multiple View Controls within it. An example of one View control can be seen below;
<asp:View ID="address_view" runat="server">
<h1>Address:</h1>
<asp:Button ID="add_new_address" CssClass="blue" runat="server" Text="Add New Address" OnClick="add_new_address_Click" />
<div id="add_address_div" runat="server">
<asp:DropDownList ID="address_dropdown_insert" runat="server">
<asp:ListItem Value="Home">Home Address</asp:ListItem>
<asp:ListItem Value="Term">Term Time Address</asp:ListItem>
<asp:ListItem Value="Mail">Mail Address</asp:ListItem>
<asp:ListItem Value="Business">Business Address</asp:ListItem>
</asp:DropDownList><br />
Address 1:
<asp:TextBox Text="" runat="server" ID="address_1TextBox" /><br />
Address 2:
<asp:TextBox Text="" runat="server" ID="address_2TextBox" /><br />
Town/City:
<asp:TextBox Text="" runat="server" ID="town_cityTextBox" /><br />
County:
<asp:TextBox Text="" runat="server" ID="countyTextBox" /><br />
PostCode:
<asp:TextBox Text="" runat="server" ID="postcodeTextBox" /><br />
Country:
<asp:TextBox Text="" runat="server" ID="countryTextBox" />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" OnClick="insert_address_button_Click" />
<asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="Button4" OnClick="cancel_address_button_Click" /><br />
</div>
<asp:ListView ID="address_list" runat="server" DataSourceID="user_address" DataKeyNames="address_id">
<EditItemTemplate>
// code here
</EditItemTemplate>
<EmptyDataTemplate>
<br />
<p>There are currently no addresses found, please click the button above to add a new address.</p>
</EmptyDataTemplate>
<ItemTemplate>
//code here
</ItemTemplate>
<LayoutTemplate>
//code here
</LayoutTemplate>
</asp:ListView>
<br />
<asp:Button CommandName="NextView" ID="Button1" runat="server" Text="Next" />
</asp:View>
The database table that the insert form inserts to is named address. It has the following fields
address_id
user_id
address_type
address_1
address_2
town_city
county
postcode
country
The user_id is pulled from another table called users.
I have the following C# to get the user_id of the current user who is logged in;
//get id of logged in user
string user_id = Session["user_id"].ToString();
My question is, how can I prevent a user from continuing onto the next view when they click Button1 if their user_id does not appear in the address table?
I'm fairly new to C# so any help would be much appreciated.
Presentationally you will want to disable Button1 until an address is entered. Initally your Button1 markup will include Enabled = "false"
<asp:Button CommandName="NextView" Enabled="false" ID="Button1" runat="server" Text="Next" />
Then within the insert_address_button_Click handler, after an address has been successfully added, enable the button like so:
protected void insert_address_button_Click(object sender, EventArgs e)
{
// ..insert the address. If successful, enable the "Next" button
Button1.Enabled = true;
}
That's a simple approach, but should do what you ask.
EDIT:
To address Button1's state in the user returns to the address view:
protected void Page_Load(object sender, EventArgs e)
{
Button1.Enabled = address_list.Items.Any();
}
Related
Currently, I'm trying to insert a new item into a drop-down list. It seems like nothing is working. Nothing is added to the dropdown list. It finds the control find I belive but does not insert a new item. Is my syntax off or something?
protected void livFact_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListViewItem item = livFact.Items[e.NewEditIndex];
DropDownList ddl = (DropDownList)item.FindControl("ddlLocation");
ddl.Items.Insert(0, new ListItem("--Location--", "0"));
}
<%--Edit Item Template--%>
<EditItemTemplate>
<tr>
<td>
<asp:Button runat="server" ID="btnModifySave" SkinID="btnListView" class="btn btn-success btn-sm" CausesValidation="true" CommandName="Update" Text="Save" ValidationGroup="ModifySave" />
<asp:Button runat="server" ID="btnModifyCancel" SkinID="btnListView" class="btn btn-danger btn-sm" CausesValidation="false" CommandName="Cancel" Text="Cancel" />
</td>
<%--Copy Area Start Set Enabled="true" --%>
<td>
<asp:DropDownList runat="server" ID="ddlLocation" class="form-control" Enabled="true" DataSourceID="sdsDropDownListLocation" DataTextField="Location" DataValueField="LocationID" AppendDataBoundItems="true" />
<asp:Label runat="server" ID="lblLocationID" Visible="false" Text='<%# Bind("LocationID") %>' />
<br />
<div class="alert alert-warning rounded">
<asp:Label runat="server" Text="Current Location editing: " />
<asp:Label runat="server" CssClass="text-primary font-weight-bold" ID="lblLocation" Text='<%# Bind("Location") %>' />
</div>
</td>
<td class="text-center">
<asp:TextBox runat="server" ID="txtFact" class="form-control" Enabled="true" Text='<%# Bind("Fact") %>' TextMode="MultiLine" Rows="5" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtFact" Enabled="true" Display="Dynamic" ErrorMessage="A Fact is required." Text="*" ForeColor="Red" SetFocusOnError="true" ValidationGroup="ModifySave" />
</td>
<%-- Copy Area End--%>
</tr>
</EditItemTemplate>
I found a simpler approach to solve my problem. Instead of using the code behind and c#, you can solve the problem on the aspx page itself. You can simply add a ListItem and put anything you want into the drop-down list on top of any data your binding to the drop-down list from a database. I would then suggest creating a Required Field Validator with an Initial Value to stop it from saving to the database if the user clicks save with the default option selected.
<asp:DropDownList runat="server" ID="ddlLocation" class="form-control" Enabled="true" DataSourceID="sdsDropDownListLocation" DataTextField="Location" DataValueField="LocationID" AppendDataBoundItems="true">
<asp:ListItem Text="--Location--" Value="-1" Selected="True" />
</asp:DropDownList>
Found my answers here.
I am trying to create a web app that can create a survey form just like google forms.
<asp:Panel ID="container" runat="server"></asp:Panel>
<asp:Panel ID="question" runat="server" Height="391px">
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Question: " Font-Size="Large"></asp:Label>
<asp:TextBox ID="txtQuestion" runat="server" Font-Size="Large"></asp:TextBox>
<asp:DropDownList ID="lstQuestionType" runat="server">
<asp:ListItem Enabled="False" Selected="True">Question Type</asp:ListItem>
<asp:ListItem>Short Answer</asp:ListItem>
<asp:ListItem>Paragraph</asp:ListItem>
<asp:ListItem>Multiple Choice</asp:ListItem>
<asp:ListItem>Check Box</asp:ListItem>
<asp:ListItem>Linear Scale</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnGenerate" runat="server" OnClick="btnGenerate_Click" Text="Generate Question" Width="137px" />
</asp:Panel>
<asp:Button ID="btnAddQuestion" runat="server" Text="Add Question" OnClick="btnAddQuestion_Click" />
and this is the C# code that generates the panel
protected void btnAddQuestion_Click(object sender, EventArgs e)
{
Panel newQuestions = new Panel();
newQuestions = question;
container.Controls.Add(newQuestions);
}
but when I click the add question button, it does not add the panel
Although i can't tell from the code probably the problem is with the question parameter. Also use MVC with controllers it will be much more clean and also VS automatically generates the View for you based on your models and controllers which i guess it will save you time in the long run.
Here's the thing. I have a ASP.NET Web Application in C#. In the Default.aspx I have an UpdatePanel containing all the elements. There, I have three validators associated with a TextBox, 4 RadioButton (two belong two one group and the other two to another group) and for each group I have a validator in order to verify if one element was checked for each group. For all RadioButton the AutoPostBack is set to "True" because when the "No" option is selected for both groups then I will enable a multi-line TextBox so the user can input some text. There's also a ValidationSummary at the end that displays error messages in a pop-up box and in the page as well. The main issue is the following: if I first select the two options from the RadioButtons and then click in the submit button, without input any text in the TextBox with the 3 validators, I will get 3 pop-up error messages. And more interesting, if I select after the 3 pop-up error messages another option from any of the RadioButton groups, I will get 4 pop-up error messages. I have a guess that it has to do with the AutoPostBack attribute of the RadioButton because it happens when I click them, but I don't know why or how to fix it.
Here's the code for Default.aspx:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="M-DCPS 7-digit Student ID#:"></asp:Label>
<asp:TextBox ID="studentIDtbox" runat="server" MaxLength="7" Width="6%" CausesValidation="false"></asp:TextBox>
<asp:RequiredFieldValidator Font-Size="Large" ID="RequiredFieldValidatorAccount" runat="server" ControlToValidate="studentIDtbox"
ErrorMessage="ID# required" ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator Font-Size="Large" ID="RegularExpressionValidator2" runat="server" ControlToValidate="studentIDtbox" ErrorMessage=" ID requires 7 digits" ValidationExpression="^[\s\S]{7,}$" ForeColor="Red">*</asp:RegularExpressionValidator>
<asp:RegularExpressionValidator Font-Size="Large" ID="RegularExpressionValidator1" runat="server" ControlToValidate="studentIDtbox"
ErrorMessage=" ID only accepts numbers" ValidationExpression="^\d+$" ForeColor="Red">*</asp:RegularExpressionValidator>
<asp:Label ID="Label2" runat="server" Text="Paper Form:"></asp:Label>
<asp:DropDownList ID="paperForm" runat="server"></asp:DropDownList>
<asp:Label ID="Label3" runat="server" Text="Paper Accommodation:"></asp:Label>
<asp:DropDownList ID="paperType" runat="server"></asp:DropDownList>
<asp:CustomValidator Font-Size="Large" runat="server" ID="cvIEP" OnServerValidate="cvIEP_ServerValidate"
Enabled="true" Display="Dynamic" SetFocusOnError="true" ErrorMessage="You must select at least one item for documentation on IEP/Section 504." ForeColor="Red">*</asp:CustomValidator>
<asp:Label ID="Label4" runat="server" Text="Paper is documented?"></asp:Label>
<asp:RadioButton ID="iepYesRb" runat="server" GroupName="IEPRequirement" Text="Yes" OnCheckedChanged="rButton_CheckedChanged" AutoPostBack="True" CausesValidation="false"/>
<asp:RadioButton ID="iepNoRb" runat="server" GroupName="IEPRequirement" Text="No" OnCheckedChanged="rButton_CheckedChanged" AutoPostBack="True" CausesValidation="false"/>
<asp:CustomValidator Font-Size="Large" runat="server" ID="cvRuse" OnServerValidate="cvRuse_ServerValidate"
Enabled="true" SetFocusOnError="true" ErrorMessage="You must select at least one item for regular use." ForeColor="Red">*</asp:CustomValidator>
<asp:Label ID="Label5" runat="server" Text="Is it used regularly?"></asp:Label>
<asp:RadioButton ID="ruseYesRb" runat="server" GroupName="RegularUse" Text="Yes" OnCheckedChanged="rButton_CheckedChanged" AutoPostBack="True" CausesValidation="false"/>
<asp:RadioButton ID="ruseNoRb" runat="server" GroupName="RegularUse" Text="No" OnCheckedChanged="rButton_CheckedChanged" AutoPostBack="True" CausesValidation="false"/>
<asp:Label ID="Label6" runat="server" Text="Provide other evidence if two 'No' where selected."></asp:Label>
<asp:TextBox ID="evidenceTbox" Style="height: 130px" runat="server" TextMode="MultiLine" Width="100%" Enabled="false"></asp:TextBox>
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submit_OnClick" />
<asp:ValidationSummary ID="valTest" runat="server" ShowMessageBox="true" BackColor="LightYellow" ForeColor="Red" Width="100%" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="submitButton" />
<asp:AsyncPostBackTrigger ControlID="iepYesRb" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="iepNoRb" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="ruseYesRb" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="ruseNoRb" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
And here's the code for Default.aspx.cs:
protected void rButton_CheckedChanged(object sender, EventArgs e)
{
evidenceTbox.Enabled = iepNoRb.Checked && ruseNoRb.Checked;
}
protected void cvIEP_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = iepNoRb.Checked || iepYesRb.Checked;
}
protected void cvRuse_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = ruseNoRb.Checked || ruseYesRb.Checked;
}
protected void submit_OnClick(object sender, EventArgs e)
{
if (Page.IsValid)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alerts", "javascript:alert('Record inserted properly')", true);
}
}
Any help will be appreciated and critics will be very welcome. Thanks.
I have created a student enrolment wizard that they go through every year that uses the MultiView control and I have multiple View Controls within it. An example of one View control can be seen below;
<asp:View ID="address_view" runat="server">
<h1>Address:</h1>
<asp:Button ID="add_new_address" CssClass="blue" runat="server" Text="Add New Address" OnClick="add_new_address_Click" />
<div id="add_address_div" runat="server">
<asp:DropDownList ID="address_dropdown_insert" runat="server">
<asp:ListItem Value="Home">Home Address</asp:ListItem>
<asp:ListItem Value="Term">Term Time Address</asp:ListItem>
<asp:ListItem Value="Mail">Mail Address</asp:ListItem>
<asp:ListItem Value="Business">Business Address</asp:ListItem>
</asp:DropDownList><br />
Address 1:
<asp:TextBox Text="" runat="server" ID="address_1TextBox" /><br />
Address 2:
<asp:TextBox Text="" runat="server" ID="address_2TextBox" /><br />
Town/City:
<asp:TextBox Text="" runat="server" ID="town_cityTextBox" /><br />
County:
<asp:TextBox Text="" runat="server" ID="countyTextBox" /><br />
PostCode:
<asp:TextBox Text="" runat="server" ID="postcodeTextBox" /><br />
Country:
<asp:TextBox Text="" runat="server" ID="countryTextBox" />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" OnClick="insert_address_button_Click" />
<asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="Button4" OnClick="cancel_address_button_Click" /><br />
</div>
<asp:ListView ID="address_list" runat="server" DataSourceID="user_address" DataKeyNames="address_id">
<EditItemTemplate>
// code here
</EditItemTemplate>
<EmptyDataTemplate>
<br />
<p>There are currently no addresses found, please click the button above to add a new address.</p>
</EmptyDataTemplate>
<ItemTemplate>
//code here
</ItemTemplate>
<LayoutTemplate>
//code here
</LayoutTemplate>
</asp:ListView>
<br />
<asp:Button CommandName="NextView" ID="Button1" Enabled="false" runat="server" Text="Next" />
</asp:View>
I have the following C# on PageLoad
protected void Page_Load(object sender, EventArgs e)
{
Button1.Enabled = address_list.Items.Any();
}
However, the button still appears even if there is no data in the ListView.
My question is, how can I prevent a user from continuing onto the next view when they click Button1 if their is no data in the ListView?
I'm fairly new to C# so any help would be much appreciated.
Try something like:
if(address_list.Items.Count() = 0)
{
Button1.enabled = false
}else
{
Button1.enabled = true
}
But have in mind that if you call this before you bind the data it wont work so be sure to not use this code as the first line in the Page load event
The lv.items.Count() will return 0 if it has no rows after binded
I have created a listbox which is populated from a db. Using modal popup and panels, this listbox appears when the select user button is clicked. When a specific user is selected from this list and the add user button is clicked, I would like to populate the labels with the specific user name and userId. I cant seem to get the labels populated. It works when i dont use modal popup. Any ideas???
my code:
<asp:Label ID="UserId" runat="server"></asp:Label>
<asp:Label ID="UserName" runat="server" Font-Bold="true" ForeColor="#97b23c" Font-Size="14px"></asp:Label>
<br />
<asp:Button ID="SelectUserBtn" runat="server" Text="Select User" />
</asp:Panel>
</td>
<td>
<asp:Panel ID="Pnl" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<h4>List Of Available Users</h4>
<asp:ListBox ID="SourceList" runat="server" DataSourceID="SqlDataSource1"
DataTextField="FullName" DataValueField="UserId" Height="160" Width="200"></asp:ListBox><br />
<asp:Button ID="OKBtn" runat="server" Text="Add User" OnClick="OkBtn_Click" />
<asp:Button ID="CancelBtn" runat="server" Text="Cancel" /><br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dmsConStr %>"
SelectCommand="SELECT [UserId], [FullName] FROM [UserProfiles]">
</asp:SqlDataSource>
</asp:Panel>
<asp:ModalPopupExtender ID="MPEUserList" runat="server" TargetControlID="SelectUserBtn" PopupControlID="Pnl" OkControlID="OKBtn" BackgroundCssClass="ModalBackground" DropShadow="true" CancelControlID="CancelBtn">
</asp:ModalPopupExtender>
my code behind:
protected void OkBtn_Click(object sender, EventArgs e)
{
UserId.Text = SourceList.SelectedItem.Value;
UserName.Text = SourceList.SelectedItem.Text;
}
Actually the ModalPopupExtender is preventing the form from postback. Just don't include OkControlID property and it should work well.
<asp:ModalPopupExtender runat="server"
ID="MPEUserList"
TargetControlID="SelectUserBtn"
PopupControlID="Pnl"
BackgroundCssClass="ModalBackground"
DropShadow="true"
CancelControlID="CancelBtn">
</asp:ModalPopupExtender>