Cannot loop through web form Control in c# - c#

I have a simple web form with some dropdown and textboxes. I am trying to get the value of the all the elements by looping through the control. (note I am assuming that I dont know the name/id of the elements and hence I am trying to loop through them to get the information and store it in a database) However the when I loop through control I get count at 5 even though there are 4 input elements and the control are represented by System.Web.UI.LiteralControl instead of System.Web.UI.WebControls.TextBox or System.Web.UI.WebControls.dropdown
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Datepicker/jquery-1.7.js" type="text/javascript"></script>
<script src="Datepicker/jquery-ui.js" type="text/javascript"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script>
$(function () {
$("#targetDueDate").datepicker();
});
</script>
<script>
$(function () {
$("#targetdeliveryoffiles").datepicker();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Tier:
</td>
<td>
<asp:DropDownList ID="tier" runat="server">
<asp:ListItem>Select Tier</asp:ListItem>
<asp:ListItem>Tier1</asp:ListItem>
<asp:ListItem>Tire2</asp:ListItem>
<asp:ListItem>Tier3</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Author:
</td>
<td>
<asp:DropDownList ID="author" runat="server">
<asp:ListItem>Select Author</asp:ListItem>
<asp:ListItem>Author1</asp:ListItem>
<asp:ListItem>Author2</asp:ListItem>
<asp:ListItem>Author3</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Quotation For:
</td>
<td>
<asp:TextBox ID="quotationFor" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Previous Est #:</td>
<td>
<asp:TextBox ID="previousEst" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
ControlToValidate="previousEst"
ValidationExpression="\d+"
Display="Dynamic"
EnableClientScript="true"
ErrorMessage="Please enter numbers only"
runat="server" ValidationGroup="a" ForeColor="Red" />
</td>
</tr>
</table>
<br />
<div style="text-align: center; width: 327px;">
<asp:Button ID="submit" runat="server" Text="Submit" CssClass="btnsubmit" ValidationGroup="a" OnClick="submit_Click" />
</div>
</div>
</form>
</body>
</html>
The onsubmit code
protected void submit_Click(object sender, EventArgs e)
{
foreach(Control c in controls)
{
if(c is System.Web.UI.WebControls.TextBox)
{
/// Do Something
}
}
}
What am i doing wrong ? How do I loop through the element to get their values ? (also the ID since I need to store both in the database)

Everything is a control in web forms; static text is LiteralControl types. So you need to check the type of control as you are and avoid Label, Literal, LiteralControl type of controls, and just look for the controls on your form. Some controls are container controls, so you may need to get into recursion to get this to work.

Related

Display data from List<string> to asp:repeater with some step

everyone!
I'm new in ASP.net dev, and I'v faced with next problem
I need to display data from List to ASP.Net page with asp:repeater with step i=i+3, but i can't use index with Container.Dataitem and repeaterId.Items.Count always is 0
I need something like that:
<itemtemplate>
<tr>
<td>
i
</td>
<td>
i+1
</td>
<td>
i+2
</td>
</tr>
</itemtemplate>
All data is type of string. I hope, that someone help me with my problem.
test this:
HTML(.aspx) FILE
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
* {font: 8pt tahoma;}
table {border-collapse:separate;border-spacing:5px;}
table td {border:solid 1px #14c739;padding:5px}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="rpt_demo">
<HeaderTemplate>
<table>
<tr>
</HeaderTemplate>
<ItemTemplate>
<td>
<%# Container.ItemIndex + 1 %>- <%# Container.DataItem %>
</td>
</ItemTemplate>
<FooterTemplate>
</tr></table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
CodeBehind File
List<String> lst_Demo = new List<string>();
lst_Demo.Add("No.1");
lst_Demo.Add("No.2");
lst_Demo.Add("No.3");
lst_Demo.Add("No.4");
lst_Demo.Add("No.5");
lst_Demo.Add("No.6");
lst_Demo.Add("No.7");
lst_Demo.Add("No.8");
rpt_demo.DataSource = lst_Demo;
rpt_demo.DataBind();

Unable to get the data from asp:textbox in Jquery popup in code behind

I want to call this Code behind method through a button which is in Jquery popup. But username and password are ended up being NULL
protected void btnSave_Click(object sender, EventArgs e)
{
string strUName= txtNewUname.Text;
string strUpass = txtNewPassword.Text;
}
Here is my Html for the Popup
<body>
<form runat="server">
<input type="button" id="btnNew" text="NEW" value="ADD" onclick="ShowAddNewPopup()"/>
<div id="alertPopup" title="Add New User" style="display :none" >
<table>
<tr>
<td class="auto-style2">UserName
</td>
<td>
<asp:TextBox runat="server" ID="txtNewUname" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Password
</td>
<td>
<asp:TextBox runat="server" ID="txtNewPassword" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button runat="server" ID="btnSave" OnClick="btnSave_Click" UseSubmitBehavior="false" Text ="save"/>
</td>
</tr>
</table>
</div>
</form>
</body>
Here is my Script to call the popup:
<head runat="server">
<title></title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script>
function ShowAddNewPopup() {
debugger;
$("#alertPopup").dialog();
}
</script>
</head>
this will work out..
$("#alertPopup").dialog({
appendTo: "form"
});

DropdownList from webservice reset after postback

My list is populated from a webservice call.
If i populate the list manually line by line its ok. I can select the item and return the selectem item with no problem e.g.
List<ListItem> oList = new List<ListItem>();
oList.Add(new ListItem("User", "0"));
oList.Add(new ListItem("Manager", "1"));
cboUsers.DataSource = oList
Thats not practical as the list I want to bind to is Dynamic, e.g.
cboUsers.DataSource = MyWebService.GetUsers() // returns List<ListItem>
No matter what I do in code I cannot get the the selected item from the list and the list ALWAYS resets itself.
Both items of code are enclosed within
if (!IsPostBack)
But when the list is bound to a web service no matter what I do (ViewState, Session anything) the list is ALWAYS reset after postback and I can NEVER get the selected item correctly.
I have tried every combination of properties on pages, controls in code and in the mark up and nothing works. I have looked at loads of articles on here and other websites and none of their examples work.
Any help would be appreciated.
[EDIT]
The full code is below.
(cboAccess, manually filled work just fine as expected)
(cboDept will fill and display but after that I can get no selection from it)
You make a selection from the dropdown (cboDept) then click the Add button (cmdAdd) which adds the text selection from the list to my database using my web service. The selection is ALWAYS shown to be the first item no matter what I select.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AttendanceWebServices.Service1Client oServices = new AttendanceWebServices.Service1Client();
List<ListItem> oList = new List<ListItem>();
oList.Add(new ListItem("Normal User", "0"));
oList.Add(new ListItem("Manager", "10"));
cboAccess.DataTextField = "Text";
cboAccess.DataValueField = "Value";
cboAccess.DataSource = oList;
cboAccess.DataBind();
cboDept.DataSource = oServices.GetTeamGroups().ToList();
cboDept.DataValueField = "Value";
cboDept.DataTextField = "Text";
cboDept.DataBind();
}
}
protected void cmdAdd_Click(object sender, EventArgs e)
{
AttendanceWebServices.Service1Client oServices = new AttendanceWebServices.Service1Client();
string sDept = ((ListItem)cboDept.SelectedItem).Text;
oServices.AddNewUser(sDept);
}
}
[EDIT2 - HTML]
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="adduser.ascx.cs" Inherits="AttendanceWeb2.adduser" %>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
<p>
</p>
<asp:scriptmanager runat="server" id="scm1">
</asp:scriptmanager>
<table class="style1">
<tr>
<td>
Name</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Department</td>
<td>
<asp:updatepanel runat="server">
<ContentTemplate>
<asp:DropDownList ID="cboDept" runat="server">
</asp:DropDownList>
</ContentTemplate>
<triggers>
<asp:AsyncPostBackTrigger ControlID="cmdAdd" />
</triggers>
</asp:updatepanel>
</td>
</tr>
<tr>
<td>
Employee ID</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Access Level</td>
<td>
<asp:dropdownlist runat="server" id="cboAccess"></asp:dropdownlist>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="cmdAdd" runat="server" onclick="cmdAdd_Click" Text="Add"
Width="100px" />
<asp:Button ID="cmdDelete" runat="server" onclick="cmdDelete_Click" Text="Delete"
Width="100px" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
[EDIT 3] - Simplified Version (Still does not work)
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="AttendanceWeb2._default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="cboDept" runat="server"></asp:DropDownList>
<asp:Button ID="cmdAdd" runat="server" Text="Add" Width="100px"
onclick="cmdAdd_Click" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AttendanceWebServices.Service1Client oServices = new AttendanceWebServices.Service1Client();
cboDept.DataSource = oServices.GetTeamGroups().ToList();
cboDept.DataValueField = "Value";
cboDept.DataTextField = "Text";
cboDept.DataBind();
}
}
protected void cmdAdd_Click(object sender, EventArgs e)
{
ListItem oItem = cboDept.SelectedItem;
string sText = oItem.Text;
string sValue = oItem.Value;
}
Make sure that you have EnableViewState set to True for your cboDept control.
EDIT
Put your table inside a form. Example:
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button type="submit" ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>

how to give print(dot matrix printer) in asp.net

In my project i have to print a form, like name, age etc. Its all printed in print paper, i just have to place name , age according to paper. currently i am using a asp.net page to serve my purpose.where i have put labels with position = absolute. when i click on print i am calling that page , in onload event i am putting label values based on previous page content.
it is simple. so is there any better way to print in dotmatrix printer or not? please suggest.
Now prnting works fine, but problem is when i click on print button i am opening that page like popup window and invoke print there. but i want that pop up window to closed after i click print or cancel button. please help me.
my code is like this:
This BtnPrint is in my main page. Main page contains inputs like name, age and etc.
In PrtPage i have placed labels according to space given for printing.So i am loding main page values in onload event of PrtPage.aspx.
protected void BtnPrint_Click(object sender, EventArgs e)
{
Response.Write("<script>");
Response.Write("window.open('PrtPage.aspx','_blank')");
Response.Write("</script>");
}
in page load of PrtPage:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<script>");
Response.Write("window.print()");
//Response.Write("window.close()");
Response.Write("<script>");
}
but whenever i click on print button it asks ' u want to close window?', so please help me in this. i want to close after i click on print or cancel in print setup window.
or suggest me if there is any good method for printing in dotmatrix..
Thanks in advance.
Sam.
What about using a single page instead of opening a pop window. In the below blog post you can see a example usage. Two div tags with printable and non printable content and the div tag with the printable content is hidden. Then you can use Jquery to print content inside a DIV.
http://itzonesl.blogspot.com/2013/02/how-to-print-content-inside-div-tag.html
Update:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server" >
<title></title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
 <script src="jquery.printElement.js" type="text/javascript"></script>
<script type="text/javascript">
  function printpage() {
$("#lblName").html($("#TextBox1").val());
$("#lblSchool").html($("#TextBox2").val());
$("#printable").printElement();
}
</script>
<style type="text/css">
#printable { display: none; }
#media print
 {
     #nonprintable { display: none; }
     #printable { display: block; }
 }
</style>
</head>
<body>
<form id="form1" runat="server">
 <div id="nonprintable">
<table class="style1">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="School"></asp:Label>:
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</div>
     <div id="printable">
<table class="style1">
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>:
</td>
<td>
<asp:Label ID="lblName" runat="server" ></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="School"></asp:Label>:
</td>
<td>
<asp:Label ID="lblSchool" runat="server" ></asp:Label>
</td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" Text="Print"
OnClientClick="printpage();" />
</form>
</body>
</html>
With MasterPage:
MasterPage.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
My ASP.NET Application
</h1>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
ContentPage.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
 <script src="jquery.printElement.js" type="text/javascript"></script>
<script type="text/javascript">
  function printpage() {
$("#MainContent_lblName").html($("#MainContent_TextBox1").val());
$("#MainContent_lblSchool").html($("#MainContent_TextBox2").val());
$("#printable").printElement();
}
</script>
<style type="text/css">
#printable { display: none; }
#media print
 {
     #nonprintable { display: none; }
     #printable { display: block; }
 }
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
 <div id="nonprintable">
<table class="style1">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="School"></asp:Label>:
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</div>
     <div id="printable">
<table class="style1">
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>:
</td>
<td>
<asp:Label ID="lblName" runat="server" ></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="School"></asp:Label>:
</td>
<td>
<asp:Label ID="lblSchool" runat="server" ></asp:Label>
</td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" Text="Print"
OnClientClick="printpage();" />
</asp:Content>
The reason you're getting those is that asp.net's scope is limited to the browser window. If the browser is set to ask the user whether they want to close the page when you try to auto-close it (which most appear to be) there's no way of blocking it from the browser.
Same thing with the print, you can't automatically print, because the browser won't let you.

Why My jQuery Accordion remove after a Button click?

I want to use jQuery Accordion in my ASP.NEt Appliacation and it works but if I click on a Button in the Accordion then my Accordion remove :( Here my Code and two pitures...
aspx:
<link href="App_Theme/mainStyle.css" type="text/css" rel="Stylesheet" />
<script src="Scripte/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="Scripte/jquery-ui-1.8.23.custom.min.js" type="text/javascript"></script>
<link href="App_Theme/jquery-ui-1.8.23.custom.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#accordion").accordion();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="manager" runat="server"></asp:ToolkitScriptManager>
<asp:UpdatePanel ID="update" runat="server">
<ContentTemplate>
<div id="accordion">
<h3><asp:Label ID="lblAbwesenheit" runat="server"/></h3>
<div>
<asp:Panel runat="server" ID="pn1" DefaultButton="btnAbwesenheitErzeugen">
<div id="Angaben" runat="server">
<table id="Angabentabelle" runat="server">
<tr>
<td><asp:Label ID="lblAbwesenheitBis" runat="server" /></td><td><asp:TextBox ID="txtAbwesenheitBis" runat="server" Enabled="false"></asp:TextBox>
<asp:ImageButton Width="20px" Height="20px" ID="imgbtnAbwesenheitBis" runat="server" ToolTip="Abwesenheit bis..." ImageUrl="~/App_Theme/Calender.ico" />
<asp:CalendarExtender ID="AbwesenheitBis" runat="server" TargetControlID="txtAbwesenheitBis"
Format="dd.MM.yyyy" PopupButtonID="imgbtnAbwesenheitBis"></asp:CalendarExtender>
</td>
</tr>
<tr>
<td><asp:Label ID="lblVertreter" runat="server" /></td>
<td>
<asp:TextBox ID="txtVertreter" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoComlete" runat="server" TargetControlID="txtVertreter"
ServiceMethod="GetCompletionList" ServicePath="" Enabled="true"
DelimiterCharacters="" UseContextKey="true" MinimumPrefixLength="1" ></asp:AutoCompleteExtender>
(Suche nach Nachname)
</td>
</tr>
</table>
<asp:Button ID="btnAbwesenheitErzeugen" runat="server" Text="Erzeugen" OnClick="btnAbwesenheitErzeugen_Click" />
<br />
<br />
</div>
<HTMLEditor:Editor ID="htmlEditAbwesenheit" runat="server" Content="<% %>" />
</asp:Panel>
</div>
<h3><asp:Label ID="lblSignatur" runat="server" /></h3>
<div>
<HTMLEditor:Editor ID="htmlEditSignatur" runat="server" Content="<% %>" />
</div>
</div>
If I start my Page I see that:
and if I click on the Button then I see that...
Why My Accordion remove?
It's because your accordion is inside of an updatepanel. When you post back, everything inside your updatepanel is removed from the DOM and readded. Hence, the DOM element that was the accordion is gone. You need to call: $("#accordion").accordion(); after your postback.
one way you could do that is to do this:
<script type="text/javascript" language="javascript">
function SetupAccordion(){
$("#accordion").accordion();
}
$(document).ready(function () {
SetupAccordion();
if (typeof Sys.WebForms != 'undefined') {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(SetupAccordion);
}
});
</script>

Categories