DataList button fires every time the page is refreshed - c#

I have a link button in a DataList in a shopping cart that works fine. Once they have selected the product, if they refresh the page, it will retrigger this button.
<asp:DataList runat="server" ID="rpCategories" RepeatColumns="4" RepeatDirection="Horizontal" OnItemCommand="rpCategories_ItemCommand"
ItemStyle-HorizontalAlign="Center" ClientIDMode="Static" ItemStyle-CssClass="bigLink">
<ItemTemplate>
<table>
<tr>
<td>
<div class="<%=EnrollmentPacksBottomBox %>">
<div class="enrollment1ProImage">
<img src="../../Common/Images/products/large/<%# Eval("largeImages") %>" width="250" />
</div>
<div class="enrollment1ProductName">
<%# Eval("productName") %>
</div>
<div class="enrollment1DescriptionBottom1">
<%# Eval("description") %>
</div>
<div class="enrollment1Price">
<table>
<tr>
<td>
<%# String.Format(CultureInfo.GetCultureInfo(MyCulture), "{0:c}", Eval("price")) %>
</td>
<td>
<div class="enrollment1PvSpacer"></div>
</td>
<td>PV:
<%# Eval("pv") %>
</td>
</tr>
</table> <%-- new amount, and buttons- start--%>
<table class="cartButtonsHolder">
<tr>
<td>
<div class="">
<button type="button" style="border: none; background-color: white;">
<table class="cartButtons">
<tr>
<td rowspan="2">
<asp:TextBox runat="server" ID="quantityHolder" Text='<%# Eval("quantity") %>' CssClass="QuantityBox"/>
</td>
<td>
<asp:LinkButton runat="server" ID="upButton" OnClick="AddItem" CssClass="caretButton fas fa-caret-up" Text="" />
</td>
</tr>
<tr>
<td>
<asp:LinkButton runat="server" ID="downButton" OnClick="SubtractItem" CssClass="caretButton fas fa-caret-down fa-m" Text="" />
</td>
</tr>
</table>
</button>
</div>
</td>
<td>
<asp:LinkButton runat="server" OnClick="SelectPro1Bottom"><div class="enrollment1SelectButton"> Add to Cart</div></asp:LinkButton>
</td>
</tr>
</div>
</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
The code behind looks like this:
protected void SelectPro1Bottom(object sender, EventArgs e)
{
DAL oDal = new DAL();
var btn = (LinkButton)sender;
//var btn = (HtmlButton)sender;
var item = (DataListItem)btn.NamingContainer; //RepeaterItem
int index = item.ItemIndex;
int quantity = 0;
GetSelected(index);
Session["amount"] = 1;
//pull in the quantity
Quantities = Session["formatQuantities"] as List<int>;
List<OrderUserTypeHolder> TempHolder = new List<OrderUserTypeHolder>();
OrderUserTypeHolder oh = new OrderUserTypeHolder();
TempHolder = Session["userTypeHolder"] as List<OrderUserTypeHolder>;
int userType = 0;
int hasAutoship = 0;
string country = "";
userType = TempHolder[0].UserType;
hasAutoship = TempHolder[0].HasAutoship;
country = TempHolder[0].Country;
ProductIdList.Clear();
using (oDal)
{
//dist
if (userType == 2 && hasAutoship == 1)
{
SqlDataReader rd = oDal.ProductsGetAllByDescLengthAutoReader(country);
while (rd.Read())
{
ProductIdList.Add(Convert.ToInt32(rd["productId"]));
}
}
else if (userType == 2)
{
SqlDataReader rd = oDal.ProductsGetAllByDescLengthDistReader(country);
while (rd.Read())
{
ProductIdList.Add(Convert.ToInt32(rd["productId"]));
}
}
//pc
else if (userType == 3)
{
SqlDataReader rd = oDal.ProductsGetAllByDescLengthPcReader(country);
while (rd.Read())
{
ProductIdList.Add(Convert.ToInt32(rd["productId"]));
}
}
//retail
else
{
SqlDataReader rd = oDal.ProductsGetAllByDescLengthRetailReader(country);
while (rd.Read())
{
ProductIdList.Add(Convert.ToInt32(rd["productId"]));
}
}
oDal.Dispose();
}
//make sure we don't have an empty array
if (Quantities.Count > 0)
{
quantity = Quantities[index];
}
else
{
quantity = 1;
}
int productId = ProductIdList[index];
AddItemsToCart(productId, quantity, hasAutoship);
}
I have tried a wide variety of fixes including feeding a value of null to my datasource, changing my view state, and moving it inside !isPostback (the link button won't work). Every time you click the linkbutton it reloads the page (does not go in !isPostback) and refreshing the page has the same effect. Every time the page is rereshed, it will add another of the last item selected to the cart. I have tried everything I could think of and everything I have found online over the past several hours.
Any suggestions would be greatly appreciated!

Related

How to get the hidden field value to be set within a table that uses pagination?

I have a repeater inside an html table that uses pagination. I want to be able to read the values of the hidden fields within the repeater in the code behind, but only the hidden field values of the table page being viewed on postback seem to be set when I'm in the code behind. How do I make it so the hidden field values are set for each repeater item regardless of the table page?
HTML:
<div id="divTableContainer" runat="server">
<table id="tblAssessmentExtracts" class="table table-striped table-sm table-hover ">
<thead>
<tr>
<th>Consumer</th>
<th>Assessment</th>
<th>Completed</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="rptAssessmentExtractQueue" runat="server" OnItemCommand="rptAssessmentExtractQueue_ItemCommand">
<ItemTemplate>
<tr class="assessment-extract-block">
<td><%# Eval("ConsumerName") %>
</td>
<td><%# Eval("SurveyName") %>
</td>
<td><%# Eval("CompletionDate") %></td>
<td class="buttoncell">
<asp:LinkButton ID="btnExportRecord" runat="server" Text="Export" CommandArgument='<%# Eval("Id") %>' />
<asp:HiddenField ID="hdAssessmentId" runat="server" Value='<%# Eval("Id") %>' />
<span class="selection"><asp:HiddenField ID="hdAssessmentSelected" runat="server" /></span>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
JavaScript:
<script type="text/javascript">
$(document).ready(function () {
var table = $('#tblAssessmentExtracts').DataTable({
"pageLength": 25,
});
});
function selectBlock(elementIndex, element) {
var selectedField = $(element).find('.selection input[type="hidden"]');
if ($(element).hasClass("table-info") === false) {
$(element).addClass("table-info");
}
if (selectedField.length > 0) {
selectedField.val("true");
}
}
function deselectBlock(elementIndex, element) {
var selectedField = $(element).find('.selection input[type="hidden"]');
if ($(element).hasClass("table-info")) {
$(element).removeClass("table-info");
}
if (selectedField.length > 0) {
selectedField.val("");
}
}
$('.assessment-extract-block').click(function (e) {
var selectedField = $(this).find('.selection input[type="hidden"]');
if (selectedField.length > 0) {
if (selectedField.val() === "true") {
deselectBlock(0, $(this));
} else {
selectBlock(0, $(this));
}
}
});
</script>
C#:
private IEnumerable<long> getSelectedResponses()
{
List<long> assessmentIds = new List<long>();
foreach (RepeaterItem riAssessment in rptAssessmentExtractQueue.Items)
{
HiddenField hdAssessmentSelected = riAssessment.FindControl("hdAssessmentSelected") as HiddenField;
HiddenField hdAssessmentId = riAssessment.FindControl("hdAssessmentId") as HiddenField;
bool export = false;
if (bool.TryParse(hdAssessmentSelected.Value, out export) && export)
{
long assessmentId;
if (long.TryParse(hdAssessmentId.Value, out assessmentId) && assessmentId > 0)
{
assessmentIds.Add(assessmentId);
}
}
}
return assessmentIds;
}

in ASP.NET Add dynamic radiobutonlist inside a div which is inside a listview

I have a listview that contains divs in rows. All divs have there own controls working. One div contains radiobuttonlist which is getting data source from codebehind. The data is populated but the radiobuttonlist is carried outside of the relevent div.
Any help will be appreciated.
<div id="riDiv" class="rightDiv" runat="server">
<asp:ListView runat="server" ID="lvItemDetails" style="border:solid; border-color:blue; border-width:10px;">
<LayoutTemplate>
<div>
<table>
<tr runat="server" id="itemplaceholder" class="TopboxDiv">
</tr>
<tr runat="server" id="itemPlaceHolder2">
</tr>
</table>
</div>
</LayoutTemplate>
<ItemTemplate>
<tr>
<div class="TopboxDiv">
<asp:Literal ID="litItemDesc" Text='<%# ".." + Eval("item_description") %>' runat="server" />
</div>
</tr>
<tr>
<div class="MidboxDivWrapper" runat="server">
<div class="leftTagDiv">
<div class="pc">Price</div>
</div>
<div id="middiv" class="MidboxDiv" runat="server">
<%--<div id="innerRadDiv">--%>
<asp:Panel runat="server" ID="pnlMidBoxDiv" BorderStyle="Solid" BorderColor="Pink" BackColor="Aqua" style="z-index:2 !important;">
<span>
<asp:RadioButtonList ID="rdCrService" runat="server" DataTextField="ServiceName" DataValueField="ServiceName" RepeatDirection="Horizontal" CssClass="radioDiv" >
</asp:RadioButtonList>
</span>
</asp:Panel>
<%--</div>--%>
<div class="radioDiv">
<asp:RadioButton id="rdPost" Text="Postoffice Price" runat="server" CssClass="radiotext" GroupName="radPriceGroup" />
<asp:Literal ID="litPost" Text="Rs. 100" runat="server" />
</div>
<%--<div class="radioDiv">
<asp:RadioButton id="rdOCS" Text="OCS Price" runat="server" CssClass="radiotext" GroupName="radPriceGroup"/>
<asp:Literal ID="litOCS" Text="Rs. 110" runat="server" />
</div>
<div class="radioDiv">
<asp:RadioButton id="rdTCS" Text="TCS Price" runat="server" CssClass="radiotext" GroupName="radPriceGroup"/>
<asp:Literal ID="litTCS" Text="Rs. 120" runat="server" />
</div>
<div class="radioDiv">
<asp:RadioButton id="rdOther" Text="Other Price" runat="server" CssClass="radiotext" GroupName="radPriceGroup"/>
<asp:Literal ID="litOther" Text="Rs. 120" runat="server" />
</div>--%>
</div>
</div>
</tr>
<tr>
<div class="bottomDivItemWrapper">
<div class="leftTagDiv">
<div class="pc">Color</div>
</div>
<div class="bottomDivItem">
<div class="radioDiv">
<%--<asp:RadioButton ID="Rad1" Text='<%# Eval("ServiceName") %>' runat="server" GroupName="testGrp" CssClass="radiotext"/>--%>
<asp:RadioButton id="rdWhite" Text="white" runat="server" CssClass="radiotext" />
</div>
</div>
</div>
<div class="OrderDivWrapper">
add cart and wishlist buttons. write cart and wishlist code functions so it can be called again and again.
</div>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
This is a pretty contrived example, but it's possible to databind the Radio Button Lists too. Below is an example; it's admittedly rather contrived, but it works.
First, the code-behind (including how I'm constructing the list). The details of getRandomString and getRandomListOfStrings aren't all that important - basically, I'm just generating random data to populate the front-end with. Obviously, you'll want to replace this with whatever your actual data source is.
private class Temp
{
public string MainName { get; set; }
// This is probably the key point here - I create a property to
// hold data to bind my radio buttons to
public List<string> RadioButtonOptions { get; set; }
}
private string getRandomString(Random r)
{
var sb = new StringBuilder();
int length = r.Next(3, 10);
for (int i = 0; i < length; i++)
{
int chr = r.Next(0, 60);
char result = (char)(33 + chr);
sb.Append(result);
}
return sb.ToString();
}
private List<string> getRandomListOfStrings(Random r)
{
int listLength = r.Next(3, 6);
var list = new List<string>();
for (int i = 0; i < listLength; i++)
{
list.Add(getRandomString(r));
}
return list;
}
protected void Page_Load(object sender, EventArgs e)
{
Random r = new Random();
var list = new List<Temp>
{
new Temp
{
MainName = getRandomString(r),
RadioButtonOptions = getRandomListOfStrings(r)
},
new Temp
{
MainName = getRandomString(r),
RadioButtonOptions = getRandomListOfStrings(r)
},
new Temp
{
MainName = getRandomString(r),
RadioButtonOptions = getRandomListOfStrings(r)
},
new Temp
{
MainName = getRandomString(r),
RadioButtonOptions = getRandomListOfStrings(r)
},
new Temp
{
MainName = getRandomString(r),
RadioButtonOptions = getRandomListOfStrings(r)
},
new Temp
{
MainName = getRandomString(r),
RadioButtonOptions = getRandomListOfStrings(r)
},
new Temp
{
MainName = getRandomString(r),
RadioButtonOptions = getRandomListOfStrings(r)
}
};
mainListView.DataSource = list;
mainListView.DataBind();
And the front end code:
<asp:ListView ID="mainListView" runat="server">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("MainName") %>' /><br />
<asp:RadioButtonList runat="server" DataSource='<%# Eval("RadioButtonOptions") %>'/><br />
<hr />
</ItemTemplate>
</asp:ListView>
The HTML for the radio buttons looks like this:
<tr>
<td><input id="mainListView_ctl01_1_0_1" type="radio" name="mainListView$ctrl1$ctl01" value="6U(" /><label for="mainListView_ctl01_1_0_1">6U(</label></td>
</tr>
And the front end looks something like this:

Checking a query Result in ASP

In my database i have a Table called News. In News i have a column named Link. This link CAN be null. If the article is written by the editors the link will be null , if the editors just want to reference an article from another site then this Link will contain a value.
My task: Make a href to the article. Here i have a problem if my editor writes the article i put a href to that article . If it is not written(so the link is not null) i have to pur that href insteaf.
I have no idea how to do this, any tips ?
Code:
Display:
<asp:DropDownList ID="DropDownSelect" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownSelect_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="DesDate"> Descending Date </asp:ListItem>
<asp:ListItem Value="AsDate"> Ascending Date </asp:ListItem>
<asp:ListItem Value="AsAlp"> Ascending Alphabetical </asp:ListItem>
<asp:ListItem Value="DesAlp"> Descending Alphabetical </asp:ListItem>
</asp:DropDownList>
<asp:ListView ID="productList" runat="server"
DataKeyNames="NewsID" GroupItemCount="1"
ItemType="SiteStiri.Models.News" SelectMethod="GetProducts">
<EmptyDataTemplate>
<table >
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td/>
</EmptyItemTemplate>
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td id="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td runat="server">
<table>
<tr>
<td>
<a href="NewsDetails.aspx?newsID=<%#:Item.NewsID%>">
<img src="/Catalog/Images/Thumbs/<%#:Item.ImagePath%>"
width="100" height="75" style="border: solid" /></a>
</td>
</tr>
<tr>
<td>
<a href="NewsDetails.aspx?newsID=<%#:Item.NewsID%>">
<p style="color: black;">
<%#:Item.NewsTitle%>
</p>
</a>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</p>
</td>
</ItemTemplate>
<LayoutTemplate>
<table style="width:100%;">
<tbody>
<tr>
<td>
<table id="groupPlaceholderContainer" runat="server" style="width:100%">
<tr id="groupPlaceholder"></tr>
</table>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr></tr>
</tbody>
</table>
</LayoutTemplate>
</asp:ListView>
</div>
Behind the page:
public IQueryable<News> GetProducts()
{
var _db = new SiteStiri.Models.NewsContext();
IQueryable<News> query = _db.News;
if ("DesDate".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderByDescending(u => u.ReleaseDate);
}
if ("AsDate".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderBy(u => u.ReleaseDate);
}
if ("AsAlp".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderBy(u => u.NewsTitle);
}
if ("DesApl".Equals(DropDownSelect.SelectedItem.Value))
{
query = query.OrderByDescending(u => u.NewsTitle);
}
return query;
}
public void DropDownSelect_SelectedIndexChanged(object sender, EventArgs e)
{
GetProducts();
productList.DataBind();
}
How can i put here <a href="NewsDetails.aspx?newsID=<%#:Item.NewsID%>"> either the page link to my site if Link IS null or the Link of the link IS NOT null?
To give a more detailed answer, set the Link column as NOT NULL and set the default value to be your URL. This was, if the user / news editor doesn't enter a value, your URL will be added to the field. The advantage of this is that you don't need any code to check for NULL etc.
Also, make sure the UI validates the URL that's added with regular expression, in case a rogue value is added.
If you want to alter it in code, you could try something like this...
public class News
{
public string Title { get; set; }
public string Url { get; set; }
public IQueryable<News> GetNews()
{
var news = new List<News> {new News {Title = "News1", Url = "NewsURL"},
new News {Title = "News1"}};
return news.AsQueryable();
}
}
You could then take out the list with no links and update them with your link...
var news = new News();
var initialNews = news.GetNews();
var newsWithLink = initialNews.Where(n => n.Url != null);
var newsWithOutLink = initialNews.Where(n => n.Url == null);
foreach (var newsItem in newsWithOutLink)
{
newsItem.Url = "MyURL";
}
var newsToDisplay = newsWithLink.Concat(newsWithOutLink);
I've just put that small example together to show how to update the links.

ASP.NET Paging next page button does not work properly

I'am trying to divide all of my products that i listed by using a repeater to pages. For example there are 3 pages. The paging looks well but when i click to next page button it is going to page 4 which is not exist actually. What can be the reason ? Implementation is below.
private void showShoes()
{
dataInThePage = new PagedDataSource()
{
DataSource = ap.getShoes(),
AllowPaging = true,
PageSize = 2,
CurrentPageIndex = pageNo
};
shoeRepeater.DataSource = dataInThePage;
shoeRepeater.DataBind();
pageAmount = dataInThePage.PageCount - 1;
//
pageInfoLabel.Text = "Page: " + (dataInThePage.CurrentPageIndex + 1) + "/" + dataInThePage.PageCount + " - Number of Shoes: " + (dataInThePage.DataSourceCount);
//
previousButton.Enabled = !dataInThePage.IsFirstPage;
nextButton.Enabled = !dataInThePage.IsLastPage;
}
private int pageNo
{
get
{
if (ViewState["pageNumber"] != null)
return Convert.ToInt32(ViewState["pageNumber"]);
return 0;
}
set
{
ViewState["pageNumber"] = value;
}
}
private int pageAmount
{
get
{
if (ViewState["pageNumber"] != null)
return Convert.ToInt32(ViewState["pageNumber"]);
return 0;
}
set { ViewState["pageNumber"] = value; }
}
public PagedDataSource dataInThePage { get; set; }
protected void previousButton_Click(object sender, EventArgs e)
{
pageNo -= 1;
showShoes();
}
protected void nextButton_Click(object sender, EventArgs e)
{
pageNo += 1;
showShoes();
}
Front-end:
<ajaxToolkit:TabPanel ID="TabPanel5" runat="server">
<HeaderTemplate>Show Shoes</HeaderTemplate>
<ContentTemplate runat="server">
<asp:Repeater ID="shoeRepeater" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table border="1" style="border-color:#ff9900; width:400px; font-weight:bold; font-family:'Oswald', Arial, sans-serif;">
<tr>
<td rowspan="6" style="width:150px; height:150px;">
<image src="shoeImages/<%#DataBinder.Eval(Container.DataItem,"ImagePath") %>"></image>
</td>
</tr>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem,"BrandName") %> - <%#DataBinder.Eval(Container.DataItem,"ModelName") %>
</td>
</tr>
<tr>
<td>
Price: $<%#DataBinder.Eval(Container.DataItem,"Price") %>
</td>
</tr>
<tr>
<td>
Size: <%#DataBinder.Eval(Container.DataItem,"Size") %>
</td>
</tr>
<tr>
<td>
Color: <%#DataBinder.Eval(Container.DataItem,"PrimaryColor") %> - <%#DataBinder.Eval(Container.DataItem,"SecondaryColor") %>
</td>
</tr>
<tr>
<td>
Quantity: <%#DataBinder.Eval(Container.DataItem,"Quantity") %>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
<div style="width:600px; height:20px;">
<div style="width:50px; height:100%; float:left">
<asp:Button ID="previousButton" runat="server" OnClick="previousButton_Click" Text="<<" Width="50px"/>
</div>
<div style="width:500px; height:100%; float:left; text-align:center">
<asp:Label ID="pageInfoLabel" runat="server" Text=" "></asp:Label>
</div>
<div style="width:50px; height:100%; float:left">
<asp:Button ID="nextButton" runat="server" OnClick="nextButton_Click" Text=">>" Width="50px"/>
</div>
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
I had to guess on a number of things, but I was able to take the posted code and get it to work in VS 2010 using an ASP.NET 4.0 project.
Here are the things I did to get it working:
Add a Shoe POCO with the 8 public properties referenced in the .aspx page (not shown)
Add a FakeShoeRepository class that has a private static List<Shoe> with 9 shoes (not shown)
Add a static method called getShoes() that returns a reference to the private member variable of the repository (not shown)
Set the dataInThePage.DataSource = FakeShoeRepository.getShoes() (not shown)
Add a Page_Load() event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pageNo = 0;
showShoes();
}
}
Remove the pageAmount property
Remove the one place pageAmount was being set in the showShoes() method (this was effectively setting ViewState["pageNumber"] = PageCount - 1)
The last two items are where the actual problem was in the provided code.

Loop through all html checkboxes in form

I would like to be able loop through all the html checkboxes in my form and if it is checked do something else do something else. I also would like to do this in the code behind not using any Javascript\jquery.
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<div>
<table width="850" border="0" cellpadding="0" cellspacing="10" class="Copy" >
<tr>
<td valign="top"><table width="200" border="0" cellspacing="0" cellpadding="3" bgcolor="#f0f4f8">
<tr>
<td width="21"> </td>
<td width="179"><strong>CheckBoxes</strong></td>
</tr>
<tr>
<td><input runat="server" type="checkbox" name="checkbox1" id="checkbox1" /></td>
<td>checkbox1</td>
</tr>
<tr>
<td><input runat="server" type="checkbox" name="checkbox2" id="checkbox2" /></td>
<td>checkbox2</td>
</tr>
<tr>
<td><input runat="server" type="checkbox" name="checkbox3" id="checkbox3" /></td>
<td>checkbox3</td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
In the codebehind I have tried a couple of different ways but i am guessing since it is an HTML Input control of type Checkbox it is not working
foreach (CheckBox chk in Page.Form.Controls.OfType<CheckBox>())
{
if (chk.Checked == true)
{
Label1.Text = "we have checkboxes";
}
else
{
Label1.Text = "Still no checkboxes";
}
}
A couple of ways:
foreach (Control item in this.form1.Controls)
{
//We just need HtmlInputCheckBox
System.Web.UI.HtmlControls.HtmlInputCheckBox _cbx = item as System.Web.UI.HtmlControls.HtmlInputCheckBox;
if (_cbx != null)
{
if (_cbx.Checked)
{
//Do something:
Response.Write(_cbx.Name + " was checked.<br />");
}
}
}
or
//We just need HtmlInputCheckBox
IEnumerable<Control> _ctrls = from Control n in this.form1.Controls where n as System.Web.UI.HtmlControls.HtmlInputCheckBox != null select n;
if (_ctrls.Count() > 0)
{
foreach (System.Web.UI.HtmlControls.HtmlInputCheckBox item in _ctrls)
{
if (item.Checked)
{
//Do something:
Response.Write(item.Name + " was checked.<br/><br />");
}
}
}
Hope this helps....
If you know the ID of the CheckBoxes, you can try using: FindControl
Control.FindControl-Methode (String)
It wouldn't be a greate solution, biut try using this:
bool cFound = true;
while(cFounnd)
{
var cCheckBox = this.FindControl(...);
if(cCheckBox != null)
{
....
}
else
{
cFound = false;
}
}
EDIT:
Maybe you try this: ControlFinder

Categories

Resources