I'm new to ASP.NET and C#, I tried few things but did not worked. I have an .aspx page where I have three buttons, as shown in below code:
<body>
<form id="htmlForm" runat="server">
<telerik:RadScriptManager ID="rsm" AsyncPostBackTimeout="1800" runat="server" />
<telerik:RadAjaxPanel ID="rap" runat="server" LoadingPanelID="ralp">
<table>
<tr>
<td colspan="2">
Invoice Download
</td>
</tr>
<tr>
<td>
Invoice Start #
</td>
<td>
<telerik:RadNumericTextBox ID="rntbStart" Runat="server" MaxValue="1000000" MinValue="1000" Width="75px" >
<NumberFormat DecimalDigits="0" GroupSeparator="" />
</telerik:RadNumericTextBox>
</td>
</tr>
<tr>
<td>
Invoice End #
</td>
<td>
<telerik:RadNumericTextBox ID="rntbEnd" Runat="server" MaxValue="1000000" MinValue="1000" Width="75px" >
<NumberFormat DecimalDigits="0" GroupSeparator="" />
</telerik:RadNumericTextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="cmdDownload" runat="server" Text="Download" />
<asp:Button ID="cmdDownloadSAP" runat="server" Text="Download SAP" />
</td>
</tr>
</table>
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
</telerik:RadAjaxPanel>
<telerik:RadAjaxLoadingPanel ID="ralp" Runat="server" Skin="Default" />
<asp:Button ID="btnConform" Visible="false" runat="server" Text="Conform Download" OnClick="btnConform_Click" />
</form>
I'm trying to do that btnConform will be initially invisible while the page is loading and now if I click cmdDownload or cmdDownloadSAP, button btnConform must get visible. Now if I click again on btnConform it will become invisible.
I cannot use JavaScript because cmdDownload and cmdDownloadSAP have some other task to complete before I make btnConform visible (say database call) and the btnConform will become visible only if the database call is successful.
partial class XPO_Accounting_InvoiceDownload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdDownload_Click(object sender, System.EventArgs e)
{
Session["InvoiceStart"] = rntbStart.Value;
Session["InvoiceEnd"] = rntbEnd.Value;
try
{
//All DB call
btnConform.Visible = true;
}
catch (Exception ex)
{
lblResult.Text = ex.ToString();
}
}
protected void cmdDownload0_Click(object sender, System.EventArgs e)
{
Session["InvoiceStart"] = rntbStart.Value;
Session["InvoiceEnd"] = rntbEnd.Value;
try
{
//All DB call
btnConform.Visible = true;
}
catch (Exception ex)
{
lblResult.Text = ex.ToString();
}
}
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
cmdDownload.Click += cmdDownload_Click;
cmdDownloadSAP.Click += cmdDownload0_Click;
}
protected void btnConform_Click(object sender, EventArgs e)
{
double InvoiceStart = (double)Session["InvoiceStart"];
double InvoiceEnd = (double)Session["InvoiceEnd"];
try
{
SqlHelper.ExecuteNonQuery(XPOStaticData.Instance.SQL, CommandType.Text, "update tblInvoiceNum set uploadedtoacct = 'Y' WHERE Status='I' AND InvoiceNum BETWEEN " + InvoiceStart + " AND " + InvoiceEnd + " and uploadedtoacct = 'N'");
lblResult.Text = "Downloaded Invoiced has conformed as the correct one.";
btnConform.Visible = false;
}
catch (Exception ex)
{
lblResult.Text = ex.ToString();
}
}
}
This does not work.
Sorry for change the content of the question (cause older content wasn't pointing the problem properly). This is actual code here I'm trying to do it. (it looks stupid to pest the complete code...)
Add the Visible property to the Button using markup to initially set the second button's visibility:
<asp:Button ID="btnOne" runat="server" Text="One" OnClick="btnOne_Click" />
<asp:Button ID="btnTwo" runat="server" Visible="False" Text="Two" OnClick="btnTwo_Click" />
And on the event handlers, add this:
protected void btnOne_Click(object sender, EventArgs e)
{
btnTwo.Visible = true;
}
protected void btnTwo_Click(object sender, EventArgs e)
{
btnTwo.Visible = false;
}
You change visibility with the Visible property:
protected void btnOne_Click(object sender, EventArgs e)
{
//require code to make it visible
btnTwo.Visible = true;
}
protected void btnTwo_Click(object sender, EventArgs e)
{
//require code to make it invisible
btnTwo.Visible = false;
}
If you want to make a server control initially invisible you also have to use this property, so you can either use codebehind to change visibility programmatically ore declaratively on the aspx-page:
<asp:Button ID="btnTwo" Visible="false" runat="server" Text="Two" OnClick="btnTwo_Click" />
One final note, Visible=false on serverside means that this control is not rendered at all on client-side. So it does simply not exist and can not be accessed (or made visible) on client-side.
If you need to access it on client-side you should make it invisible via stylesheets either by using
display: none(does not take up any space) or
visibility: hidden(still takes up space in the layout).
Update acc. to the last edit of your question the event handlers don't seem to be registered anymore.
So you need:
<asp:Button ID="cmdDownload" OnClick="cmdDownload_Click" runat="server" Text="Download" />
<asp:Button ID="cmdDownloadSAP" OnClick="cmdDownload0_Click" runat="server" Text="Download SAP" />
Related
Hi i am busy creating a asp.net project that needs to get three values from the user. I am using a textbox with a range textmode to get these values and the first time the user moves the slider it works perfectly but after that the user can.t adjust any of the textboxes anymore.
I tried using a slider extender but this does not seem to work and only a textbox shows.
my html code to create the textboxes and labels showing the value
body>
<form id="form1" runat="server">
<p>
Quantum 1 Length:
<asp:TextBox ID="txtVal1" runat="server" TextMode="Range" min="1" Max="8" OnTextChanged="txtVal1_TextChanged1" AutoPostBack="True"></asp:TextBox>
<asp:Label ID="lblVal1" runat="server" Text="/////"></asp:Label>
</p>
<p>
Quantum 2 Length:
<asp:TextBox ID="txtVal2" runat="server" TextMode="Range" min="1" Max="8" OnTextChanged="txtVal2_TextChanged1" AutoPostBack="True"></asp:TextBox>
<asp:Label ID="lblVal2" runat="server" Text="/////"></asp:Label>
</p>
<p>
Quantum 3 Length:
<asp:TextBox ID="txtVal3" runat="server" TextMode="Range" min="1" Max="8" AutoPostBack="True" OnTextChanged="txtVal3_TextChanged1"></asp:TextBox>
<asp:Label ID="lblVal3" runat="server" Text="/////"></asp:Label>
</p>
<p>
<asp:Button ID="btnNext" runat="server" AutoPostBack="true" Height="24px" OnClick="btnNext_Click" style="margin-left: 0px" Text="Next" Width="150px" />
<script type="text/javascript" __designer:mapid="129">
function NextPage() {
window.open('Execution.aspx', 'Execution');
}
</script>
<asp:Label ID="lblError" runat="server" ForeColor="Red" Text="Invalid input; Please ensure that Quantum 1 <= Quantum 2 <= Quantum 3" Visible="False"></asp:Label>
</p>
</form>
</body>
My C# code used to control the boxes and change their values
public partial class Quantums : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
txtVal1.Text = HttpContext.Current.Session["Q1"].ToString();
}
catch { }
try
{
txtVal2.Text = HttpContext.Current.Session["Q2"].ToString();
}
catch { }
try
{
txtVal3.Text = HttpContext.Current.Session["Q3"].ToString();
}
catch { }
lblVal1.Text = txtVal1.Text;
lblVal2.Text = txtVal2.Text;
lblVal3.Text = txtVal3.Text;
//UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger() { ControlID = "btnNext" });
}
protected void txtVal1_TextChanged1(object sender, EventArgs e)
{
HttpContext.Current.Session["Q1"] = txtVal1.Text;
}
And then the same code for textbox 2 and 3. Any suggestions on why the textboxes vaklue keeps resetting and autopostback is enabled on all three buttons.
I just noticed you were missing if(Page.IsPostBack()) return;. Your problem is that every time you change your textbox, the 'Page_Load' event is enacted, then the respective event handler (see page life cycle). So you were setting the text to the sessions, then setting the sessions to the text, effectively doing nothing.
You actually don't need anything for this page in the Page_load method, at least for now. Try the following:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtVal1_TextChanged(object sender, EventArgs e)
{
Session["Q1"] = txtVal1.Text;
lblVal1.Text = txtVal1.Text;
}
protected void txtVal2_TextChanged(object sender, EventArgs e)
{
Session["Q2"] = txtVal2.Text;
lblVal2.Text = txtVal2.Text;
}
protected void txtVal3_TextChanged(object sender, EventArgs e)
{
Session["Q3"] = txtVal3.Text;
lblVal3.Text = txtVal2.Text;
}
You can then access the user input values using the respective session values. For example:
protected void Submit(object sender, EventArgs e)
{
List<string> answers = new List<string>();
answers.Add(Session["Q1"].ToString());
answers.Add(Session["Q2"].ToString());
answers.Add(Session["Q3"].ToString());
// Do stuff with answers
}
I have enabled required field validator for txtProjectDate textbox, which is not allowing the calendar to be poped-up after clicking the button for calendar.So, i cannot select values to be entered in to the textbox.
ERROR:Mandatory Field
ASPX FILE
<tr>
<td>Project Started at</td>
<td>::</td>
<td><asp:TextBox ID="txtProjectDate" runat="server" OnTextChanged="txtProjectDate_TextChanged1"></asp:TextBox>
<asp:ImageButton ID="btnShowCalendar" runat="server" OnClick="btnShowCalendar_Click" ImageUrl="D:\calendar4.png" />
<asp:Calendar ID="calProjectDate" runat="server" OnSelectionChanged="calProjectDate_SelectionChanged" Visible="False"></asp:Calendar>
<asp:RequiredFieldValidator ID="rfvProjectDate" runat="server" ControlToValidate="txtProjectDate" ErrorMessage="MANDATORY"></asp:RequiredFieldValidator>
</td>
</tr>
ASPX.CS CODES FOR textbox,calendar,button
protected void btnShowCalendar_Click(object sender, ImageClickEventArgs e)
{
calProjectDate.Visible = true;
}
protected void calDateofBirth_SelectionChanged(object sender, EventArgs e)
{
txtProjectDate.Text = calProjectDate.SelectedDate.ToShortDateString();
calProjectDate.Visible = false;
}
protected void txtProjectDate_TextChanged(object sender, EventArgs e)
{
}
protected void calProjectDate_SelectionChanged(object sender, EventArgs e)
{
txtProjectDate.Text =
calProjectDate.SelectedDate.ToShortDateString();
calProjectDate.Visible = false;
}
Pls help me to find out the solution...
Simply set CausesValidation attribute to false in ImageButton:-
<asp:ImageButton ID="btnShowCalendar" runat="server" CausesValidation="false"
OnClick="btnShowCalendar_Click" ImageUrl="D:\calendar4.png" />
Guys before asking this questions I did a lot of research on this topic but I am not able to detect the problem.I have radio button in the datalist and I am trying to get the selected radio button value.But it is showing me false for all radio button on submit button.My datalist is like this:
<asp:DataList ID="dlEmails" RepeatLayout="Flow" runat="server" >
<HeaderTemplate>
<table>
<tr>
<th>Select Email Address </th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:RadioButton ID="rbtnSelect" Text='<%#Eval("Emails") %>' onclick='fnrad(this);' GroupName="a" Checked='<%#Eval("Primary") %>' runat="server" /><br />
(<asp:Label ID="lablel" runat="server" Text='<%#Eval("Verified") %>'> </asp:Label>)
</td>
<td valign="middle">
<asp:Label ID="lblID" Style="display: none;" runat="server" Text='<%#Eval("Id") %>'> </asp:Label>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Main") %>'> </asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
Javascript for allowing only single radio button selection at a time is like this:
<script>
function fnrad(rbtn) {
var radioList = document.getElementsByTagName("input");
for (var i = 0 ; i < radioList.length; i++) {
if (radioList[i].type == "radio") {
radioList[i].name = 'a';
radioList[i].checked = false;
}
}
rbtn.checked = true;
rbtn.setAttribute("Checked","checked");
}
</script>
And my code behind is like this:
public partial class Member_EmailList : System.Web.UI.Page
{
EmailsBAL _mbl = new EmailsBAL(SessionContext.SystemUser);
DataSet _ds = new DataSet();
URLMessage URLMessage = new URLMessage();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
_mbl.LoadByUser(_ds, 1);//SessionContext.SystemUser;
dlEmails.DataSource = _ds.Tables[_mbl.SqlEntityX];
dlEmails.DataBind();
}
protected void lnkConfirm_Click(object sender, EventArgs e)
{
RadioButton rb;
Label lbl;
int id = 0;
foreach (DataListItem di in dlEmails.Items)
{
rb = (RadioButton)di.FindControl("rbtnSelect");
if (rb.Checked == true)
{
lbl = (Label)di.FindControl("lblID");
id = WebHelper.Cast(lbl.Text, 0);
}
}
//Response.Redirect("~/Member/ConfirmEmail.aspx?" + URLMessage.Encrypt("SystemUser=" + SessionContext.SystemUser + "Id=" + id.ToString()));
}}
I tried your javascript function and experienced your problem
I would like to suggest to use Jquery on your javascript function. I refactored your jav fuction to this.
function fnrad(rbtn) {
$('input').removeAttr('checked');
$(rbtn).prop('checked', true);
}
This works perfectly on my end. Please let me know if you still encountering the issue.
Have landed into a scenario again. The summary is as follows:
I have a user-control which is basically a mix up of a textbox,imagebuttons,checkboxlist incorporated together to look like a single-select dropdown with checkboxes..works pretty fine. One of the images inside the usercontrol opens up an aspx page as a popup.have few functionalities there viz saving values to database and stuffs.
On the OK button click of the popup page, I should be able to save the values to the DB as well as populate the usercontrol(which acts as a dropdown) with the value which I have saved to the DB.
Here the problem arises, when trying to bind the checkboxlist(present in the usercontrol) to the values from the DB, I get the error that the checkboxlist object is null and has not been created.
I feel that on the OK button click, the usercontrol has to refresh and hence the checkboxlist will be active.
PFB the relevant codes:
Usercontrol.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="SingleSelectCustomDropDown.ascx.cs" Inherits="MS.IT.Informa.UI.UserControls.SingleSelectCustomDropDown" %>
<asp:Panel ID="panel" runat="server">
<div id="FirstDiv">
<table>
<tr>
<td align="right">
<asp:TextBox ID="txtSelect" runat="server" ReadOnly="true"></asp:TextBox>
</td>
<td>
<asp:Image ID="imgShow" ImageUrl="../Images/DDGlyph.png" onmouseover="this.src='../Images/DDGlyphHOVER.png'" onmouseout="this.src='../Images/DDGlyph.png'" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<div id="SecondDiv" style="display:none;">
<asp:CheckBoxList ID="chkBoxList" runat="server">
<asp:ListItem Value="0" Text="Standard" Selected="True"></asp:ListItem>
</asp:CheckBoxList>
</div>
<div id="ThirdDiv" style="display:none;">
<asp:ImageButton ID="btnNew" runat="server" Height="20px" ImageUrl="~/Images/new.png" Width="20px" OnClientClick="ShowPopup();" />
<asp:ImageButton ID="btnEdit" runat="server" Height="20px" ImageUrl="~/Images/edit.png" Width="20px" />
<asp:ImageButton ID="btnDefault" runat="server" Height="20px" ImageUrl="~/Images/default.png" Width="20px" />
<asp:ImageButton ID="btnDelete" runat="server" Height="20px" ImageUrl="~/Images/delete.png" Width="20px" />
</div>
</td>
</tr>
</table>
</div>
</asp:Panel>
<script type="text/javascript">
//Displays the divs containing checkboxlist and images
function ShowList() {
document.getElementById("SecondDiv").style.display = "block";
document.getElementById("ThirdDiv").style.display = "block";
}
//Hides the divs containing checkboxlist and images
function HideList() {
document.getElementById("SecondDiv").style.display = "none";
document.getElementById("ThirdDiv").style.display = "none";
}
//Displays the selected item from the checkboxlist into the textbox placed in the Custom Control
function DisplaySelectedItem(sender, txtBoxID) {
var x = document.getElementById(sender.id);
var chkBoxPrefix = sender.id + "_";
var selectedText;
for (i = 0; i < x.rows.length; i++) {
if(document.getElementById(chkBoxPrefix+i).checked)
{
selectedText = document.getElementById(chkBoxPrefix+i).parentNode.innerText;
}
}
document.getElementById(txtBoxID.id).value = selectedText;
}
//Ensures that only one item is selected from the checkboxlist
function SelectOnlyOneCheckBox(e) {
if (!e) e = window.event;
var sender = e.target || e.srcElement;
if (sender.nodeName != 'INPUT') {
return;
}
var checker = sender;
var chkBox = document.getElementById('<%= chkBoxList.ClientID %>');
var chks = chkBox.getElementsByTagName('INPUT');
for (i = 0; i < chks.length; i++) {
if (chks[i] != checker)
chks[i].checked = false;
}
}
function ShowPopup() {
window.open("ViewColumnOptions.aspx", "ViewColumnOptions", "height=300,width=600,left=300,top=150");
}
</script>
The codebehind for the usercontrol as below:
public partial class SingleSelectCustomDropDown : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
chkBoxList.Attributes.Add("onclick", "SelectOnlyOneCheckBox(event);DisplaySelectedItem(this," + txtSelect.ClientID + ");HideList();");
txtSelect.Attributes.Add("onclick", "ShowList();");
imgShow.Attributes.Add("onclick", "ShowList();");
}
}
public void PopulateOtherViews()
{
SaveReportViewFilter<ReportFilterBase> newObj = new SaveReportViewFilter<ReportFilterBase>();
ViewColumnOptions vwobj = new ViewColumnOptions();
newObj.UserName = vwobj.Page.User.Identity.Name;
SaveReportView<ReportFilterBase> obj2 = new SaveReportView<ReportFilterBase>();
DataTable dt = obj2.GetSaveReportViewFromDataBase(newObj);
chkBoxList.DataSource = dt;//chkBoxList becomes null here..we have ample data in the datatable though
chkBoxList.DataTextField = dt.Columns[0].ToString();
chkBoxList.DataValueField = dt.Columns[0].ToString();
chkBoxList.DataBind();
}
}
The function PopulateOtherViews is called on the button click of the aspx page.
Below is the code:
protected void btnOK_Click(object sender, EventArgs e)
{
if (chkSaveView.Checked)
{
if (!string.IsNullOrEmpty(txtViewName.Text))
{
//some code here to save the view name from txtViewName to the DB
SingleSelectCustomDropDown obj22 = new SingleSelectCustomDropDown();
obj22.PopulateOtherViews();
Page.ClientScript.RegisterStartupScript(this.GetType(),"close","CloseWindow();",true);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "alertEnterViewName", "alertMessage('Please enter the view name');", true);
}
}
}
Any help, suggestions, pointers will be greatly appreciated.
Regards
Anurag
In btnOK_Click you are creating a new instance of the user control and not attaching it to the page. My suggestion is:
1.Register the user control and add it to the page.
<%# Register Src="~/UserControl/SingleSelectCustomDropDown.ascx" TagPrefix="uc1" TagName="SingleSelectCustomDropDown" %>
and ...
<uc1:SingleSelectCustomDropDown runat="server" id="obj22" />
2.Now modify in code behind:
protected void btnOK_Click(object sender, EventArgs e)
{
if (chkSaveView.Checked)
{
if (!string.IsNullOrEmpty(txtViewName.Text))
{
//some code here to save the view name from txtViewName to the DB
//Do not create the control again
//SingleSelectCustomDropDown obj22 = new SingleSelectCustomDropDown();
obj22.PopulateOtherViews();
Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "CloseWindow();", true);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "alertEnterViewName", "alertMessage('Please enter the view name');", true);
}
}
}
Thanks in advance for your help.
I am using c#.net.
I have two views on my webpage (contained within one multiview), both contain buttons.
view_1
Contains a repeater/datasource and an custom made ‘edit’ button (which holds the ID for each row returned).
view_2
Contain an ‘update’ form and a ‘update’ button. When the user presses the update button the information within the database for that particular row is updated.
The problem I believe lies with my ‘update’ button within view_2
Code behind (‘update’ button), I have an if statement:
protected void Page_Load(object sender, EventArgs e)
{
updateSuccessFailed.Visible = false;
if (!Page.IsPostBack)
{
_multiView1.ActiveViewIndex = 0;
}
}
protected void update_Click(object sender, EventArgs e)
{
var Id = Convert.ToInt32((ID.Value));
notYetUpdated.Visible = true;
updateSuccessFailed.Visible = false;
tblV updateV = new tblV();
updateV.vID = venueId;
updateV.vame = updateName.ToString();
updateV.vPostcode = updatePropPostcode.ToString();
if (vRepos.Update(updateV))
{
notYetUpdated.Visible = false;
updateSuccessFailed.Visible = true;
updateMessage.Text = "Updated.";
}
else
{
notYetUpdated.Visible = false;
updateSuccessFailed.Visible = true;
updateMessage.Text = "An error has occurred, please try again.";
}
}
_view2
<asp:View ID="_view2" runat="server">
<div style="text-align:center" runat="server" id="notYetUpdated">
<table border="0" cellspacing="1">
<tr>
<td style="text-align:left;">Name</td>
<td style="text-align:left;"><asp:TextBox ID="updateName" MaxLength="60" runat="server" /></td>
</tr>
<tr>
<td style="text-align:left;">Postcode</td>
<td style="text-align:left;"><asp:TextBox ID="updatePropPostcode" MaxLength="60" runat="server" /></td>
</tr>
</table><br />
<asp:Button ID="updateVCancel" Text="Cancel" runat="server" onclick="cancel_Click" CssClass="standardButton" />
<asp:Button ID="updateVConfirm" Text="Update" runat="server" onclick="update_Click" CssClass="standardButton" />
<asp:HiddenField ID="vUpdateID" runat="server" />
</div>
<div style="text-align:center" runat="server" id="updateSuccessFailed">
<p><asp:Label ID="updateMessage" runat="server" /></p>
<asp:Button ID="updateBack" Text="Back To Start" runat="server" onclick="backToStart_Click" CssClass="standardButton" />
</div>
</asp:View>
notYetUpdated / updateSuccessFailed are div’s which hold different information.
When the user first ‘updates’ a record it make the right div visible. (notYetUpdated – holds the form information / updateSuccessFailed – holds a message to state whether the record has been updated or not). However when you access the view_2 again it accesses the update_Click event and updateSuccessFailed is visible even though it shouldn’t be.
I thought I could clear all stored information within the viewstates with the code below, however this is not working.
ViewState.Clear();
ClearChildViewState();
Thanks
Clare :-)
The 4th line should be updateSuccessFailed.Visible = false;?
This was an error on my part. I adapted my code, here it is:
var Id = Convert.ToInt32((ID.Value));
tblV updateV = new tblV();
updateV.vID = venueId;
updateV.vame = updateName.ToString();
updateV.vPostcode = updatePropPostcode.ToString();
notYetUpdated.Visible = false;
updateSuccessFailed.Visible = true;
if (vRepos.Update(updateV))
{
updateMessage.Text = "Updated.";
}
else
{
updateMessage.Text = "An error has occurred, please try again.";
}
Hope this helps other people.