accessing checkbox inside listview? - c#

i have a group of listviews with a checkbox in each listview as:
<telerik:RadListView Skin="Vista" ID="RadListView3" DataSourceID="SqlDataSource_subentry"
runat="server" ItemPlaceholderID="EmployeesContainer" DataKeyNames="ID">
<LayoutTemplate>
<fieldset id="FieldSet1">
<legend>Issues</legend>
<asp:PlaceHolder ID="EmployeesContainer" runat="server" ></asp:PlaceHolder>
<br />
<br />
</fieldset>
</LayoutTemplate>
<ItemTemplate>
<div style="width:200px; height:165px; -webkit-border-radius: 20px;-moz-border-radius: 20px; border-radius: 20px;
border:2px solid black; background-color:#00FFFF; text-align:center; margin-top:20px; margin-bottom:10px;
margin-left:20px; ">
<br />
<div style=" width:190px; height:auto; font-size:small;color:#000099; text-align:left; margin-left:12px;"><%#Eval("Worked_Project")%>
<label id="Label1" runat="server"><%# Eval( "ID" ) %></label>
<asp:CheckBox ID="MyCheckBox" runat="server" Text=<%# Eval( "ID" ) %>/></div>
<div style="width:190px; height:auto; font-size:small;color:black;text-align:center; padding-left:2px; margin-left:5px;" ><%# DataBinder.Eval(Container.DataItem, "Week_Ending", "{0:d/M/yyyy }")%></div>
<div style=" width:190px; height:75px; font-size:large; color:#000099; text-align:center; "><%#Eval("Activity")%> </div>
<div style=" width:190px; height:auto; font-size:small;color:black; text-align:left; margin-left:12px; margin-bottom:5px; "><%#Eval("UserId")%> </div>
</div>
</ItemTemplate>
on a button click i want to pick the id assocaiated with a particular checkbox as:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control ctl in RadListView3.Controls)
{
CheckBox chk = (CheckBox)ctl.FindControl("MyCheckBox");
if (chk.Checked== true)
{
string value = chk.Text.ToString();
Session["id"] = value.ToString();
Label2.Text = Session["id"].ToString();
}
}
}
but it is giving error on line of code:
if (chk.Checked== true)
and the error is
Object reference not set to an instance of an object.
plzz help me out

Try this:
protected void RadListView1_ItemCommand(object sender,Telerik.Web.UI.RadListViewCommandEventArgs e)
{
CheckBox button = (CheckBox)e.ListViewItem.FindControl("CheckBox1");
if (e.CommandName == "Approve")
{
if (button.Checked == true)
{
//your code
}
else
{
//your code
}
}
}
or if you need to pick id on Button click only (loop) follow below link.
http://www.telerik.com/community/forums/aspnet-ajax/listview/get-selected-value-of-radlistview.aspx

You have to check type of this control first
CheckBox chk = ctl as CheckBox ;
if (chk !=null)
{
//do something
}
more info. about as
keyword

Related

Issue grabbing correct control/update panel in repeater

I'm having issues with dynamically updating a drop down list control when it is inside a repeater.
Basically I have a repeater and inside that repeater are 2 drop down lists. The one list is defined in my aspx page, the other drop down list is inside an update panel where I want to be able to have it dynamically update based on the selection of the first drop down list. I think part of my problem is the updatepanel is getting confused because I have more than one repeater item?
Here is the code for my repeater:
<asp:Repeater ID="billingTemplate" runat="server" OnItemDataBound="template_ItemDataBound">
<ItemTemplate>
<tr style="font-size: 100%" runat="server">
<td colspan="4" style="width: 100%; vertical-align: top">
<div class="panel panel-default panel-billing">
<asp:Panel CssClass="row panel-heading panel-heading-billing text-left" ID="headingBilling" ClientIDMode="Static" runat="server">
<div class="col-xs-1">
<input type="hidden" id="templateUK" runat="server" value='<%#Eval("templateUK")%>' />
</div>
<div class="col-sm-3">
<label for="ddlInvFilterType" class="col-sm-4 control-label text-right labelCls testclass">Filter Type:</label>
<div class="col-sm-8">
<asp:DropDownList runat="server" ID="ddlInvFilterType" ClientIDMode="Static" placeholder="Choose Filter Type" CssClass="form-control smallSize FilterType" AutoPostBack="true" OnSelectedIndexChanged="ddlFilterType_SelectedIndexChanged">
<asp:ListItem Value="">- None -</asp:ListItem>
<asp:ListItem Value="RevType1">Revenue Type 1</asp:ListItem>
<asp:ListItem Value="RevType2">Revenue Type 2</asp:ListItem>
<asp:ListItem Value="RevType3">Revenue Type 3</asp:ListItem>
<asp:ListItem Value="ServiceTeams">Service Team</asp:ListItem>
</asp:DropDownList>
</div>
</div>
<asp:UpdatePanel ID="InvFilterValuePanel" ClientIDMode="Static" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<div class="col-sm-3">
<label for="ddlInvFilterValue" class="col-sm-4 control-label text-right labelCls">Filter Value:</label>
<div class="col-sm-8">
<asp:DropDownList runat="server" ID="ddlInvFilterValue" ClientIDMode="Static" placeholder="Choose Filter Value" CssClass="col-sm-6 form-control smallSize">
<asp:ListItem Value="">- None -</asp:ListItem>
</asp:DropDownList>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlInvFilterType" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
<asp:Panel CssClass="panel-collapse collapse" ID="collapseBilling" ClientIDMode="Static" runat="server">
<div class="panel-body">
<table class="table table-condensed table-bordered" style="margin: 0; padding: 0; border-top: none; border-bottom: none; border-left: thin; border-right: thin">
<tbody>
<%-- other controls --%>
</tbody>
</table>
</div>
</asp:Panel>
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
Heres the code for my selected index change:
protected void ddlFilterType_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DropDownList ddl = (DropDownList)sender;
string ddlClass = ddl.CssClass;
string[] classes = ddlClass.Split(' ');
string repeaterClass = classes[classes.Length - 1];
string repeaterID = "0";
string appendStr = "";
if (repeaterClass.Contains("templateID"))
{
repeaterID = repeaterClass.Substring(repeaterClass.Length - 1);
appendStr = "_" + repeaterID;
}
foreach (RepeaterItem item in billingTemplate.Items)
{
HtmlInputHidden hidden = item.FindControl("headingBilling").FindControl("templateUK") as HtmlInputHidden;
if (hidden.Value == repeaterID)
{
DropDownList d1 = item.FindControl("headingBilling").FindControl("ddlInvFilterType") as DropDownList;
DropDownList d2 = item.FindControl("headingBilling").FindControl("ddlInvFilterValue") as DropDownList;
if (d1.SelectedValue.Length > 0)
{
d2.Items.Clear();
d2.Items.Add(new ListItem(" - None - ", ""));
switch (d1.SelectedValue)
{
case "ServiceTeams":
foreach (var pair in serviceTeamsController.GetAllServiceTeamsLOVs())
{
if (!String.IsNullOrWhiteSpace(pair.Value))
d2.Items.Add(new ListItem(pair.Value, pair.Key));
}
break;
default:
foreach (var pair in masterController.GetMasterLOVs(filterTypeDict[d1.SelectedValue]))
{
if (!String.IsNullOrWhiteSpace(pair.Value))
{
d2.Items.Add(new ListItem(pair.Value, pair.Key));
}
}
break;
}
}
else
{
d2.Items.Clear();
d2.Items.Add(new ListItem(" - None - ", ""));
}
}
}
}
catch (Exception ex)
{
}
}
Heres a screenshot of what I'm looking at when theres more than one repeater item:
Basically whats happening now is if I update the filter type in item2 it will update the filter value in item 1. If I update the filter type in item1 it will update the filter value in item 1 as expected.
What I want is to be able to update item2 filter type and then the filter value in item 2 is updated accordingly.
Anyone have any ideas on what I could be missing?
So ended up getting this to work, I think the main issue was the clientidmode was confusing the update panel somehow, so I removed that and made the update panel go around both drop downs. I also didnt end up needing the trigger so I removed that as well.

RadComboBox of telerik lost SelectedValue

I have a RadComboBox of Telerik that lost the selectedValue, when I try to insert in DB. It has the value at all time during the execution, but when I click the save button I see that the RadComboBox lost the SelectedValue only, it doesn't lose the text only the int value. I use asp.net and c#.
Please help me with your comments and recommendation. Thanks.
<!-- Definition of cbProg in page aspx-->
<tr style="display: table-row;">
<td style="display: table-cell; vertical-align: inherit">
<div style="left: 210px; position: absolute; top: 140px;">
<label for="cbProg">Programa:</label>
<div style="left: 65px; position: absolute; top: -10px;">
<telerik:RadTextBox ID="txtProg" runat="server" Width="40px" Height="25px" Enabled="true" MaxLength="2"
BorderColor="#C9D9F8" BorderStyle="Solid" Skin="Silk" ToolTip="Código del Programa al que se asigna el gasto."
BorderWidth="1px" ReadOnly="false">
</telerik:RadTextBox>
<div style="left: 45px; position: absolute; top: 0px;">
<telerik:RadComboBox ID="cbProg" runat="server" RenderMode="Lightweight" EnableLoadOnDemand="true" Filter="StartsWith"
OnSelectedIndexChanged="cbProg_SelectedIndexChanged" AutoPostBack="true"
EmptyMessage="-- Select --" HighlightTemplatedItems="true" Skin="Metro" Width="230px" ToolTip="Nombre del programa al que se asigna el gasto.">
</telerik:RadComboBox>
<div style="left: 195px; position: absolute; top: 5px;">
<asp:RequiredFieldValidator ID="rfvPrograma" runat="server" ControlToValidate="cbProg"
ForeColor="Red" Font-Bold="true" SetFocusOnError="True" ValidationGroup="Guardar"></asp:RequiredFieldValidator>
</div>
<div style="left: 235px; position: absolute; top: 1px;">
<asp:ImageButton ID="btPrograma" runat="server" Text="" OnClick="btPrograma_Click"
ImageUrl="../Imagenes/find1.png" Width="24px" Height="24px" ></asp:ImageButton>
</div>
</div>
</div>
</div>
</td>
</tr>
//Method for list all programs of table in cbProg
public void ListarProgramas()
{
query = #"SELECT IdLinea, IdPrograma, Descripcion
FROM PRE_Programas";
cbComun(cbProg, query, "Descripcion", "IdLinea");
}
//Method for fill all comboBox
public void cbComun(RadComboBox cb, string str, string Texto, string Valor, int a=1 )
{
DataTable dt = dtSelec(str);
cb.Items.Clear();
cb.DataTextField = Texto;
cb.DataValueField = Valor;
cb.DataSource = dt;
cb.DataBind();
}
//Assigned Id Number of Program to other textbox filltering with SelectedValue of cbProg
protected void cbProg_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
query = #"SELECT IdLinea, IdPrograma, Descripcion FROM PRE_Programas WHERE IdLinea='" + cbProg.SelectedValue + "' ";
txtComun(txtProg, query, "IdPrograma");
}
Try binding the RadComboBox in the Page_Init event.

RadTextBox not updating

Hi I am trying to update some database values with a simple form with Telerik RadTextBoxes. I can set the text value no problem but when it comes to saving on button click the values do not get updated. My code to set the textbox is in the Page Load function however I am checking if(!IsPostBack)
Here is my html:
<div style="width:95%;">
<div style=" width:100%;float:left; background-color:#fff; border:1px solid #d1d2d3; margin-top:25px; padding:15px; -webkit-box-shadow: inset 1px 0px 5px 0px rgba(50, 50, 50, 0.10); -moz-box-shadow: inset 1px 0px 5px 0px rgba(50, 50, 50, 0.10); box-shadow: inset 1px 0px 5px 0px rgba(50, 50, 50, 0.10);">
<h2 style="float:left; width:50%;"><asp:Literal ID="litTitle" runat="server" Text="Add A New Zone" /></h2>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Width="100%">
<telerik:RadNotification runat="server" id="notifyError" Position="Center" Width="330" Height="130" EnableRoundedCorners="true" EnableShadow="true" Style="z-index: 35000"></telerik:RadNotification>
<div style="float:right;margin-right:10px;margin-left:5px;"><telerik:RadButton ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /></div><div style="float:right;margin-right:5px;"><telerik:RadButton ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" /></div><div style="float:right;margin-right:5px;"><telerik:RadButton ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" OnClientClicked="confirm('Are you sure you wish to delete this zone?');" /></div>
<div class="clear" style="clear:both;height:25px;"></div>
<div style="width:100%;">
<div style="float:left;">
<div style="float:left; margin-left:10px; margin-right:5px; width:200px;">Zone Code:</div>
<div style="float:left; margin-left:5px; margin-right:10px;"><telerik:RadTextBox ID="txtZoneName" runat="server" CausesValidation="false" Width="200"></telerik:RadTextBox></div>
</div>
<div class="clear" style="height:35px;clear:both;"></div>
<div style="float:left;">
<div style="float:left; margin-left:10px; margin-right:5px; width:200px;">Name:</div>
<div style="float:left; margin-left:5px; margin-right:10px;"><telerik:RadTextBox ID="txtZoneCode" runat="server" CausesValidation="false" Width="200"></telerik:RadTextBox></div>
</div>
<div class="clear" style="height:35px;clear:both;"></div>
<div>
<div style="float:left; margin-left:10px; margin-right:5px; width:200px;">Monthly Book Page Goal:</div>
<div style="float:left; margin-left:5px; margin-right:10px;"><telerik:RadTextBox ID="txtZoneBookGoal" runat="server" CausesValidation="false" Width="200"></telerik:RadTextBox></div>
</div>
<div class="clear" style="height:45px;clear:both;"></div>
<div style="width:250px; margin:0 auto;">
<div style="float:left;"><telerik:RadButton ID="btnCreate" runat="server" Text="Save" OnClick="btnCreate_Click" /></div>
</div>
</div>
</telerik:RadAjaxPanel>
</div>
</div>
Here is my c#:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
if(Request.Params["z"] != null)
{
Zones z = Zones.GetByID(Utils.GetLong(Request.Params["z"]));
this.txtZoneCode.Text = z.Code;
this.txtZoneName.Text = z.Name;
this.txtZoneBookGoal.Text = Utils.GetString(z.BookGoal);
this.litTitle.Text = "Edit Zone";
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
bool valid = true;
string error = "";
if (this.txtZoneCode.Text == "")
{
valid = false;
error += "- Code<br/>";
}
if (this.txtZoneName.Text == "")
{
valid = false;
error += "- Name<br/>";
}
if (valid)
{
if (Request.Params["z"] != null)
{
Zones z = Zones.GetByID(Utils.GetLong(Request.Params["z"]));
z.Code = this.txtZoneCode.Text;
z.Name = this.txtZoneName.Text;
z.BookGoal = Utils.GetInt(this.txtZoneBookGoal.Text);
z.Save();
if (z.ZoneID > 0)
{
Response.Redirect("ManageZones.aspx?v=1", true);
}
else
{
this.notifyError.Title = "Zone update was not successfull!";
this.notifyError.Text = "There was an error saving your zone. Please try again";
this.notifyError.Show();
return;
}
}
else
{
Zones z = new Zones();
z.Code = this.txtZoneCode.Text;
z.Name = this.txtZoneName.Text;
z.BookGoal = Utils.GetInt(this.txtZoneBookGoal.Text);
z.Save();
if (z.ZoneID > 0)
{
Response.Redirect("ManageZones.aspx?v=1", true);
}
else
{
this.notifyError.Title = "Zone creation was not successfull!";
this.notifyError.Text = "There was an error saving your zone. Please try again";
this.notifyError.Show();
return;
}
}
}
else
{
this.notifyError.Title = "Please fill in the following to continue:";
this.notifyError.Text = error;
this.notifyError.Show();
return;
}
}
Robert Zeno
I changed OnClientClicked to OnClientClicking because there was a javascript error causing the page to break and not pull the correct data
You have four buttons - two buttons have same name Save.
Look like you are pressing the wrong. It makes me confuse too.
protected void btnSave_Click(object sender, EventArgs e)
{
// Not this event
}
protected void btnCreate_Click(object sender, EventArgs e)
{
// You need to use this event.
}
For better design practice
Use CSS instead of inline styles.
You need to name buttons properly. For example, SaveRadButton, CreateRadButton
DO NOT use Hungarian notation such as btnXXX especially when you use third party controls like telerik.

ASP.NET Passing variable from .aspx page to UserControl

I have a usercontrol that I want to pass a value to from my .aspx page. I've setup a property in the usercontrol to set to the value I need from the page but the value is always null.
Here is my control code in the .aspx page,
<tcs:SubmitDataDiscrepancy runat="server" ID="SubmitDataDiscrepancy" EntityNameProp='<%# Bind("Association_Name") %>'/>
the .ascx code,
<div style="text-align:right;">
<asp:LinkButton ID="ReportLink" runat="server" Text="Report data discrepancy"></asp:LinkButton>
<asp:Panel ID="ReportPanel" runat="server" CssClass="modalPopup" style="width:auto;">
<div id="PopHeader" style="background-color: black; color: white; height: auto;">
Report discrepancy
<div style="float: right;">
<asp:LinkButton runat="server" ID="AssociationCancelTextButton" CausesValidation="False" Font-Underline="false"
CssClass="glyphicon glyphicon-remove white" ToolTip="Close" />
</div>
</div>
<div style="margin:10px;">
<div>
Submit change request for: <asp:Label ID="EntityName" runat="server" />
</div>
<div>
<textarea id="reportTextArea" runat="server" class="form-control" style="height: 100px; margin-bottom:10px; width: 100%;"></textarea>
<button id="reportSubmitButton" runat="server" class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
and the .ascx.cs page,
public partial class SubmitDataDiscrepancy : System.Web.UI.UserControl
{
public string EntityNameProp { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
EntityName.Text = EntityNameProp;
}
}
Why am I not getting the value I'm expecting from the .aspx page?
All variables are disposed at the end of the page's lifecycle, that's the stateless nature of HTTP. So you need a way to persist your property. You could for example use ViewState, Session or a HiddenField (Other ways).
In this case it makes sense to simply use the Label.Text:
public partial class SubmitDataDiscrepancy : System.Web.UI.UserControl
{
public string EntityNameProp {
get { return EntityName.Text; }
set{ EntityName.Text = value; }
}
}

How to pass the count of checkbox checked in listview C#

Good Afternoon ,
I have collection of checkboxes under Listview .
Can u suggest me how to pass the count of checked and unchecked checkbox items .
Label count has to be updated when checkbox is checked or unchecked .
My code
<ItemTemplate>
<li>
<p style="text-align: left; padding: 3px;">
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Title")%>' CssClass="imglbl" /><br />
<img src="<%#Eval("ThumbNail")%>" style="vertical-align: middle;" height="120" width="110" id="myimg" title="Click to Play" /><br />
<asp:Label ID="lblpricetag" Text="Price($):" runat="server" Style="float: left; display: inline; margin-top: 8px;" /><asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>' CssClass="imgprice" /><br />
<br />
<%--<input type="checkbox" id="chkaddtocart" value='<%#Eval("id")%>' name="chkaddtocart" onchange="chkonclick(this.checked,this.value);" />Add to Cart--%>
<asp:CheckBox ID="chkaddtocart" runat="server" Onclick=function() Text="Add to Cart" />
</p>
</li>
</ItemTemplate>
I got it in script but i want it in codebehiend
this is my script code
function chkonclick(o,chkvalue) {
//alert(o + "," + chkvalue);
if (o) {
myitems += 1;
}
else {
if (myitems > 0)
myitems -= 1;
else
myitems = 0;
}
myitemslabel = myitems + " items";
$("#txtitems").val(myitems.toString());
$("#lblitems").html(myitemslabel);
}
change your checkbox like this
<asp:CheckBox ID="chkFocusArea" runat="server" OnCheckedChanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />
You can make use of checkbox change event
protected void chkFocusArea_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
//update label count if checkbox is checked
}
You can provide a name for the checkbox as test
var list=input[name=test]
var count = 0;
var total = 0;
$(list).each(function() {
if(this.checked) count++;
total++;
});
you can use these two variables for displaying what you want.

Categories

Resources