populate a label from a listbox using panels/modal popup - c#

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>

Related

How to disable a button if no items displayed in a ListView

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

Preventing User from Continuing without adding data to a database table

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();
}

Filtering Repeater in ASP.NET

I'm new to ASP.NET and C# and I am having difficulty filtering my SQL data within a Repeater Control using AJAX.
I have the SQL data showing on my page but I need to find a way to filter the data using AJAX. I'm not sure if I need to add code to my .cs file or can update through JavaScript/JQuery.
The code for Repeater is below:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<div class="PhysicianDetails">
<span><%#Eval("LastName")%>,</span>
<span><%#Eval("FirstName")%></span>
<p><%#Eval("Language")%></p>
<asp:Button ID="Button2" runat="server" Text="View Profile" CommandName="ViewProfile" CommandArgument='<%#Eval("ContactID")%>' />
</div>
</ItemTemplate>
</asp:Repeater>
The code for input fields and DropDownList which I want to filter the data within Repeater Control.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="sidebar">
<h3>Refine Your Search</h3>
<p>Provider First Name:</p>
<input type="text" runat="server" name="name" value=" " placeholder="Search by First Name" />
<asp:Button ID="Button1" runat="server" Text="GO" />
<p>Provider Last Name:</p>
<input type="text" name="name" value=" " placeholder="Search by Last Name" />
<%--Last Name<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>--%>
<asp:Button ID="Button2" runat="server" Text="GO" />
<p>Search by Language:</p>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Languages" DataValueField="Languages"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DB %>" SelectCommand="Select DISTINCT Language From Languages"></asp:SqlDataSource>
<asp:Button ID="Button3" runat="server" Text="GO" />
</div>
Any suggestions? Thanks!

TextBox OnTextChanged / PostBack not working

My problem is that I can't seem to get my textbox to fire the OnTextChanged event. I also checked and it doesn't seem like it is doing an autopostback despite the fact that it is set to true.
Basically, I am trying to validate the text in the textbox and either enable or disable a button based on the validation status.
Here's my code:
<asp:Panel ID="panelAccessControl" runat="server" Visible="false">
<asp:Panel ID="panelAddMileageRate" runat="server" BorderWidth="2" >
<h2>Add Mileage Rate</h2>
<asp:ScriptManager ID="scriptManagerMileageRate" runat="server" />
<asp:UpdatePanel ID="updatePanelAddMileageRate" runat="server">
<ContentTemplate>
<p>
Purpose:
<asp:DropDownList ID="ddlAddPurpose" runat="server"
AutoPostBack="true" Width="200px"></asp:DropDownList>
<br />
Country:
<asp:DropDownList ID="ddlAddCountry" runat="server"
AutoPostBack="true" Width="200px" >
</asp:DropDownList>
<br />
Effective Date:
<asp:Calendar ID="calAddEffectiveDate" runat="server">
</asp:Calendar>
<br />
Mileage Rate:
<asp:TextBox ID="txtAddMileageRate" runat="server"
AutoPostBack="true" Width="200px"
CausesValidation="true"
ontextchanged="txtAddMileageRate_TextChanged">
</asp:TextBox>
<asp:RegularExpressionValidator
ID="validatorAddMileageRate"
ControlToValidate="txtAddMileageRate" runat="server"
SetFocusOnError="true"
ErrorMessage="Only Numbers allowed"
ValidationExpression="^\d{1,20}(\.\d{0,4})?$">
</asp:RegularExpressionValidator>
</p>
</ContentTemplate>
</asp:UpdatePanel>
<p>
<asp:Button ID="buttonAddSecurityGroup" runat="server"
Text="Submit" onclick="buttonAddSecurityGroup_Click" />
</p>
</asp:Panel>
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
</asp:Panel>
The text box in question is txtAddMileageRate.
Set the EventName property for your txtAddMileageRate AsyncPostBackTrigger to TextChanged.
Add following codes inside your update panel and outside of ContentTemplate
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtAddMileageRate"
EventName="TextChanged" />
</Triggers>
Other sugguestion:
Have you tried looking at the AutoComplete (http://www.asp.net/ajax/ajaxcontroltoolkit/samples/autocomplete/autocomplete) control that is part of the AjaxControlToolKit? Its behaves the same way you want your solution to behave.

c# UpdatePanel button click won't work

Why my buttons won't work in update panel, but if I press "enter" key it's working?
<asp:ScriptManager ID="Sqrpt1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel UpdateMode="Always" ChildrenAsTriggers="true" ID="updpan" runat="server"><ContentTemplate>
<fieldset>
<asp:Panel runat="server" ID="ClientSearchPa" DefaultButton="SearchClientPopup">
<asp:TextBox ID="SearchClientBox" runat="server"></asp:TextBox>
<asp:Button ID="SearchClientPopup" runat="server" Text="Search"
onclick="SearchClientPopup_Click" /></asp:Panel>
<br />
<asp:ListBox ID="Clients" runat="server" Height="341px" Width="682px"></asp:ListBox>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<asp:Button ID="ClientSelect" runat="server" OnClick="ClientSelect_Click" Text="button" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
Your code is perfectly alright and button are firing event on server side change your some control values in server events. Your might not be noticing very fast response of ajax call
protected void SearchClientPopup_Click(object sender, EventArgs e)
{
SearchClientBox.Text = "Hello ajax SearchClient clicked";
}
protected void ClientSelect_Click(object sender, EventArgs e)
{
SearchClientBox.Text = "Hello ajax ClientSelect cliecked ";
}
<asp:Panel runat="server" ID="ClientSearchPa" DefaultButton="SearchClientPopup">
<asp:TextBox ID="SearchClientBox" runat="server"></asp:TextBox>
<asp:Button ID="SearchClientPopup" runat="server" Text="Search" onclick=
"SearchClientPopup_Click" />
</asp:Panel>
Here DefaultButton is set to SearchClientPopup.So if the focus is on any control within the Panel,then Enter key will work and SearchClientPopup will fire the click event.
<asp:Button ID="ClientSelect" runat="server" OnClick="ClientSelect_Click" Text="button" />
This button is not inside the panel control.So you have to explicitly fire it by clicking

Categories

Resources