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.
Related
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.
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.
'
I have a problem with my site in ASP.NET. My applications contains a few buttons (imagebuttons) that are able to change. For example: When I press an empty button I want to be able to put a picture and a website in it. I've made this possible with a popupbox.
The problem where I'm running into:
When Im trying to give button 2 a image and a website, it gives button 1 a image and website because I told it in:
public partial class Ingelogd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Image1_Click(object sender, ImageClickEventArgs e)
{
if (DropDown.SelectedItem.Value == "Youtube")
{
Btn_1.ImageUrl = "~/Images/Youtube.png";
Btn_1.PostBackUrl = ("http://www.youtube.com");
Btn_1.OnClientClick = "";
}
else
{
}
}
Now I actually want the same for Btn_2 , but then I have to write the exact same code but change Btn_1 to Btn_2. This is impossible to do because I want 19 website ( youtube but also facebook, twitter etc.) Im also going to have 19 buttons ( currently I only have Btn_1 and Btn_2). This would mean I have to make 19*19 = 361 pieces of code. I assume there is a way to make a sub program for this. My teacher also told me I could use cookies for the buttonclick, but I have no idea how to make a cookie with a Imagebutton.
What is the best solution to solve this problem?
I also have the ASP.NET Code here for you who wants to see that code aswell.
<title>Ingelogd</title>
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if (e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
//-->
</script>
<style type="text/css">
#popupBoxOnePosition{
top: 0; left: 0; position: fixed; width: 100%; height: 120%;
background-color: rgba(0,0,0,0.7); display: none;
}
.popupBoxWrapper{
width: 550px; margin: 50px auto; text-align: left;
}
.popupBoxContent{
background-color: #FFF; padding: 15px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
Dit is de pagina als je ingelogd bent.
</div>
<div id="popupBoxOnePosition">
<div class="popupBoxWrapper">
<div class="popupBoxContent">
<h3>Instellingen</h3>
<p>Kies uit een van de volgende links.</p>
<asp:DropDownList ID="DropDown" runat="server">
<asp:ListItem >Select</asp:ListItem>
<asp:ListItem >Youtube</asp:ListItem>
<asp:ListItem >Facebook</asp:ListItem>
</asp:DropDownList>
<br />
<asp:ImageButton ID="Button1" runat="server"
OnClick="Image1_Click"/>
<br />
<asp:Button ID="Button_afsluiten" runat="server" Height="48px" OnClientClick="toggle_visibility('popupBoxOnePosition');return false;" Text="Afsluiten" />
</div>
</div>
</div>
<asp:ImageButton ID="Btn_1" runat="server" Height="48px" OnClientClick="toggle_visibility('popupBoxOnePosition');return false;"
ImageUrl="" />
<asp:ImageButton ID="Btn_2" runat="server" Height="48px" OnClientClick="toggle_visibility('popupBoxOnePosition');return false;"
ImageUrl="" />
</form>
</body>
</html>
You can try to add attributes to DropDownList and to use it. For example add two new attributes ImageUrl and SiteUrl. Your code should look:
protected void Image1_Click(object sender, ImageClickEventArgs e)
{
Btn_1.ImageUrl = DropDown.Attrubutes["ImageUrl"];
Btn_1.PostBackUrl = DropDown.Attrubutes["SiteUrl"];
Btn_1.OnClientClick = "";
}
I've never used asp, but most of those code is just plain c#, so I should be good.
Make 19 separate functions, one for each button, and dynamically add the Image and Url instead of using ifs:
protected void Image1_Click(object sender, ImageClickEventArgs e)
{
String val = DropDown.SelectedItem.Value;
Btn_1.ImageUrl = "~/Images/" + val + ".png";
Btn_1.PostBackUrl = "http://www." + val + ".com";
Btn_1.OnClientClick = "";
}
If you want 1 function for all buttons as well, you'll have to give all your buttons a Name and check the senders name inside the function.
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; }
}
}
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