I have an ASP:DataList. Inside the DataList, I have the code below which displays the Name and Checkbox for each row.
What I would like to do is:
Store the Name in a hidden field.
Loop through all the checkboxes, find the ones that are checked and INSERT the value into the database.
If possible, please provide some sample code.
<td style="width: 600px"><%#Eval("Name></td>
<td style="width: 20px">
<asp:CheckBox ID="chkName" Text='<%# Eval("Name") %>' runat="server" />
</td>
what you are doing is fine.
Just in your postback, loop though the check box and check
foreach (Checkbox cb in YOurcheckboxlist)
{
if (cb.Checked) {
// get the name and insert
}
}
Had this situation before, this is what I did,
in the form_Sumbit event,
foreach (Control ctl in form1.Controls)
{
if (ctl is CheckBox)
{
//check for checked or not and store the value into an array or a List.
}
}
Not a perfect solution, let's see if someone can come up with a better idea.
Related
This is what I wrote inside the Repeater
<td style="text-align: center; width: 60px">
<asp:CheckBox ID="DeleteCheckBox" runat="server" CommandArgument='<%# Eval("Id")%>' AutoPostBack="True" OnCheckedChanged="DeleteCheckBox_Click" />
td>
and this is the C#
protected void DeleteLinkbtn_Click(object sender, EventArgs e)
{
try
{
LinkButton BtnId = (LinkButton)(sender);
string ButtonId = BtnId.CommandArgument;
long Active = 1;
if (ButtonId !=string.Empty)
{
if (DeleteCheckBox.Checked==true)
{
objdalTransactionEntry.RentSettingsIsActiveUpdate(Convert.ToInt64(ButtonId), Active);
}
}
}
catch (Exception) { }
}
The Problem is that it is not being able to find the ID of the checkbox DeleteCheckBox in C# code
Based on you code, I have the following comments:
Change you function name, it's confusing. It should either be DeleteCheckBox_Changed or it should not be used for the OnCheckedChanged event
Your sender is not a LinkButton it is the checkbox itself. So you can access the checkbox control with CheckBox checkBoxThatChanged = (CheckBox)(sender)
DeleteCheckBox does not really exist. You have multiple DeleteCheckBoxs due to your repeater, so you won't be able to access it this way.
If you are indeed using a LinkButton, and you want to find a checkbox, then you should add a value to the link button, indicating the row of the table it refers to and iterate through all the checkbox controls until you find your target one.
I realise that there have been similar questions which can be resolved with an ID reference and .FindControl(ID) but that solution won't work for me as I am doing an iterative setting.
Situation is this. I have a 4 column table. one of which is hidden and only shown based on a date. To show the column I iterate through the table and cells making them visible. This process also needs to activate a RequiredFieldValidator that is needed for each cell if the column is shown. Here is a sample Cell from the .aspx
<asp:TableCell runat="server" visible="false">
<div class="pull-right form-group form-inline">
<asp:Label ID="Label28" CssClass="control-label" runat="server" Text="Sales Actual:"></asp:Label>
<asp:TextBox ID="txtStationarySalesFull" runat="server" CssClass="form-control input-sm form-control-inline-small"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator38" Display="Dynamic" CssClass="help-block" runat="server" ErrorMessage="" ValidationGroup="Form" ControlToValidate="txtStationarySalesFull" Enabled="false"></asp:RequiredFieldValidator>
</div>
</asp:TableCell>
To achieve this I was trying:
foreach (TableRow row in tblGoals.Rows)
{
foreach (TableCell cell in row.Cells)
{
cell.Visible = true;
foreach (RequiredFieldValidator fv in cell.Controls)
{
fv.Enabled = true;
}
}
}
This doesn't work though. It will find the controls collection fine, but of the 5 controls returned there are no RequiredFieldValidators in the collection and it fails out with :
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.RequiredFieldValidator'.
The controls collection contains two LiteralControls, a TextBox and two Text values. Weirdly the second text="" is actually the requiredfieldvalidator I am looking for.
Any ideas how I can actually make this work? I would rather like to avoid referencing each control by ID as this is mostly going to be reused code.
I can think of a messy version involving
foreach (TableCell cell in row.Cells)
{
cell.Visible = true;
if (cell.Controls.Count > 4) {
RequiredFieldValidator fv = (RequiredFieldValidator)cell.Controls[3];
fv.Enabled = true;
}
}
But that doesn't catch all cases. There are some cells where there are less controls that need the validator enabled.
Linq, ever to the rescue. cell.Controls.OfType<RequiredFieldValidator> should return all controls in that collection that are of type RequiredFieldValidator.
https://msdn.microsoft.com/en-us/library/bb360913(v=vs.110).aspx
Further to #Will's answer, your loop could become like this:
foreach (TableRow row in tblGoals.Rows)
{
foreach (TableCell cell in row.Cells)
{
cell.Visible = true;
foreach (RequiredFieldValidator fv in cell.Controls.OfType<RequiredFieldValidator>)
{
fv.Enabled = true;
}
}
}
In the third foreach loop, you have to filter only controls of type RequiredFieldValidator
I have a standard HTML <table inside an ASP:Panel control.
Inside this HTML table is a <TR row
And inside this TR row are severale tables.
<asp:panel ID="PanelPreStart" runat="server" Visible="false" Enabled="false" Width="500px" >
<table runat="server" id="tblPreStart">
...
...
<tr id="trRiskMgt" runat="server" visible="false">
<td colspan="2">
<strong><big>** RISK MANAGEMENT **</big></strong>
<br /><br />
<table id="tblHOC_MON" runat="server" visible="false">
..
..
</table>
<br />
<table id="tblRMP_TUE" runat="server" visible="false" style="width: 100%; height: 114px; color: black; background-color: white;">
<tr>
...
... etc
How do I iterate through the trRiskMgt row to get to all the tables (tblHOC_MON, etc)?
I've tried this but it does not work. I get this error:
CS1579: foreach statement cannot operate on variables of type
'System.Web.UI.HtmlControls.HtmlTableRow' because
'System.Web.UI.HtmlControls.HtmlTableRow' does not contain a public
definition for 'GetEnumerator'
foreach (HtmlTableRow trow in trRiskMgt)
{
foreach (HtmlTable tbl in trow.Cells)
{
foreach (HtmlTableRow row in tbl.Controls)
{
foreach (HtmlTableCell cell in row.Cells)
{
foreach (Control ctrl in cell.Controls)
{
//CONTROL IS TEXBOXT: DISABLE CONTROL (NOT HIDE!)
if (ctrl is TextBox)
{
TextBox txt = (TextBox)ctrl;
txt.Enabled = wsDisable;
}
}
}
}
}
}
Ideas for both JavaScript and C# code behind appreciated.
Thank you
[UPDATE]
I've managed to get around it by hardcoding one of the above tables into the code...
foreach (HtmlTableRow row in tblHOC_MON.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
foreach (Control ctrl in cell.Controls)
{
//CONTROL IS TEXBOXT: EXTRACT VALUES//
if (ctrl is TextBox)
{
TextBox txt = (TextBox)ctrl;
txt.Enabled = false;
}
}
}
}
This is obviously not ideal, as I will need to repeat this code multiple times for every table in the TR row.
But it will do for now.
Try inner html concept.
Example:
to iterate particular html element say "table row" in your case, write below code on your controller:
trRiskMgt.InnerHtml = Server.HtmlEncode("your data");// elementId.InnerHtml
If you haven't specified any data it will show the single row in this case.
AS the data will increase it will iterate the row.
You can further google HtmlEncode or Decode for better understanding.
ASPX Page:
<asp:ListView ID="lvSubjects" runat="server" >
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbRegularSubjects" Text=<%# Eval("SubjectName") %> runat="server" />
</ItemTemplate>
<AlternatingItemTemplate>
<asp:CheckBox ID="cbRegularSubjects" Text=<%# Eval("SubjectName") %> runat="server" />
</AlternatingItemTemplate>
</asp:ListView>
Code Behind:
For Each ctrl As Control In Page.Controls
If TypeOf ctrl Is CheckBox AndAlso CType(ctrl, CheckBox).Checked Then
'**Here I want to get the text of the check box and insert into the DB**
End If
Next
Where am i going wrong??i do not get any error...but this code is not working for me.
For i As Integer = 0 To lvSubjects.Items.Count - 1
Dim coll As ControlCollection = lvSubjects.Items(i).Controls
For Each c As Control In coll
If TypeOf c Is CheckBox Then
Dim box As CheckBox = CType(c, CheckBox)
If box.Checked Then
MsgBox(box.Text)
End If
End If
Next c
Next i
you are only searching in Page.Controls whereas your checkboxes are inside deeper in page control hierarichy.
foreach (ListViewItem row in listView.Rows)
{
if (row.ItemType == ListViewItemType.DataItem)
{
CheckBox chk = row.FindControl("Checkboxid");
if (chk.Checked)
{
//Write code to store this checkbox value to database here
}
}
}
Please change the code in VB with proper control name
I want to store the repeater control value to database table. I have a repeater control with three lable with corresponding textbox.
<asp:Repeater ID="RepeatInformation" runat="server">
<ItemTemplate>
<tr>
<td valign="top" class="style3">
<%#DataBinder.Eval(Container,"DataItem.fldleavename")%>
</td>
<td valign="top">
<asp:TextBox ID="TextBox1" runat="server" CssClass="textbox" ></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
repeater control format:
Casual Leave : Textbox1
Medical Leave : Textbox1
Annual Leave : Textbox1
how can i store the repeater value to database. I don't have an idea for storing this value please help me ..
foreach (RepeaterItem item in RepeatInformation.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
TextBox textBox = (TextBox)item.FindControl("TextBox1");
//Now you have your textbox to do with what you like
//You can access the .Text property to find the new value that needs saving to the database
}
}
On a side note. I would consider revising the names of your controls. It'll make life a lot easier in the future if you adopt some sort of convention and use camel case.
With regards to saving this to a database - it entirely depends on how you're dealing with data access at the moment. That's another question altogether I think.
You can store the value in list object & then bind it into data table in database.
Modify your datatable and updated into database using SQLDataAdapter
enter code here
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem != null)
{
//Create object for database
// Get Data from rep controls
// using e.Item
// store it into database
}
}