OpenWindow-Opens two windows on one click - c#

when I click on the following button it opens up the page twice. I can not seem to find the error:
the .cs file:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
btnPrint.Attributes.Add("onclick", "openWindow(" + Request.QueryString["cId"].ToString() + "," + Request.QueryString["aId"].ToString() + ");");
}
}
The javascript:
<script type="text/javascript">
//Function To Open A New Window To View the selected PDF
function openWindow(cId, aId) {
window.open("printMe.aspx?cId=" + cId + "&aId=" + aId + "", "", "", "");
}
</script>
and the button in the aspx:
<dx:ASPxButton ID="btnPrint" runat="server" Image-Url="~/images/print.bmp" ToolTip="Printer Friendly Page">
</dx:ASPxButton>

changing the javascript like below fixed the problem. I am no expert at javascript.
<script type="text/javascript">
//Function To Open A New Window To View the selected PDF
function openWindow(cId, aId) {
window.open("printMe.aspx?cId=" + cId + "&aId=" + aId,"mywindow");
}
</script>

Related

add dynamic button click event c#

with json data, created a table with for loop and added dynamic buttons in different conditions, am looking how to add one onclick event for all buttons and get the value of clicked button value or id.
Thank you for your help
dynamic jsondata = JsonConvert.DeserializeObject(responseData);
for (int i = 0; i < jsondata.val.Count; i++)
{
htmlTable.AppendLine("<tr>");
htmlTable.AppendLine("<td>"+jsondata.val[i].id+"</td>");
htmlTable.AppendLine("<td>"+jsondata.val[i].Name+"</td>");
htmlTable.AppendLine("<td>"+jsondata.val[i].desc+"</td>");
string getStatus = GetStatus(url);
if(getStatus=="0")
htmlTable.AppendLine("<td>" + "<input id='ctb"+i+ "' type='button' value='START'></td>");
else if(getStatus=="1")
htmlTable.AppendLine("<td>" +"<input id='ctb"+i+ "' type='button' value='STOP'></td>");
else
htmlTable.AppendLine("<td>" + "<label> Error Occured </label></td>");
htmlTable.AppendLine("</tr>");
}
htmlTable.AppendLine("</table>");
litTable.Text = htmlTable.ToString();
This is a simple demo, But you can do the same in your scenario as well. I just implemented this sample in WebForm. If I am mistaken do comment please.
public partial class Sample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
divContainer.InnerHtml = "<div id='mysamplecontrol' onclick='thisismyclick(this);'>Start</div>";
}
}
<form id="form1" runat="server">
<div runat="server" id="divContainer"></div>
<script>
function thisismyclick(control) {
alert(control.id);
}
</script>
</form>

In C#, How can I make the user's input of one text field appear in a string on another text field without overwriting the current content?

I need the user input to appear after the word 'assigned'. I tried:
$(document).ready(function()
{
$('#txtInstructions').keyup(function (e) {
var txtVal = $(this).val();
$('#StrautoDoc').val(txtVal);
});
});
but this was overwriting the current text I have generated via C#, which has information I am pulling from a database.
C# string I'm generating:
StrautoDoc.Text = DateTime.Now.ToString("MM/dd/yy") + " - " + GetUserName() + " assigned " + [user input for the task summary] + " to " [person being assigned the task];
This C# file is synced with a .aspx file where 'txtInstructions' (an
E.g.
private string GetTaskSummary()
{
tbltask currentTask = new tblTask();
currentTask.Instructions = txtInstructions.Text;
if (currentTask != string.Empty)
{
currentTask.Instructions = Convert.ToString(txtInstructions.Text);
}
return currentTask;
}
'tblTask' & 'Instructions' are from a db. But nothing seems to work, I was wondering what I am doing wrong, and what I can do to get this to work properly. Thanks in advance!
You need to rethink your whole approach. ASP.NET was created so that you do not have to work with HTML/JavaScript directly.
You can create the text boxes in your ASPX file as follows:
<asp:TextBox runat="server" ID="txtInstructions" OnTextChanged="txtInstructions_TextChanged" AutoPostBack="true" />
<asp:TextBox runat="server" ID="StrautoDoc" />
The in your code behind file, you can have the following:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtInstructions_TextChanged(object sender, EventArgs e)
{
StrautoDoc.Text = DateTime.Now.ToString("MM/dd/yy") + " - " + GetUserName() + " assigned " + txtInstructions.Text + " to [person being assigned the task]";
}
private string GetUserName()
{
return "name";
}
This works entirely in C# without you having to write any JavaScript as ASP.NET was intended. Is there any reason why you want to be using Jquery seeing that your entire problem can be solved in ASP.NET alone?

How to update ASP Web application Literal or Label after user presses button

Warning: I am a novice when it comes to ASP and javascript - I'm more used to desktop apps. Web development is completely new to me.
I have inherited an ASP.net project that I need to keep up-to-date.
My current problem is that I need to display the client time in a text control (label or literal control - doesn't have to specifically be one of those, I just need to show it in text) when the user has clicked a button to 'Save'. If I do it server-side, in the 'SaveChanges' function, I get the time of where the server is.
My button is defined as below:
<asp:Button ID="Save" runat="server" Text="Save Changes" OnClick="SaveChanges"
ValidationGroup="ProjectSummaryValidationGroup"
meta:resourcekey="SaveResource1" />
And my Literal/Label is:
<asp:Label ID="SaveTime" runat="server"></asp:Label>
I have found a javascript function to calculate the client time from one of the other questions on here: (EDIT: I have updated this function so that the text value of my label is being assigned a value)
<script type="text/javascript">
function GetDate(date) {
CurTime = new Date(date);
var offset = (new Date().getTimezoneOffset() / 60) * (-1);
var utc = CurTime.getTime() + (offset * 60000 * (-1));
var serverDate = new Date(utc + (3600000 * offset));
var dateString = (serverDate.getMonth() + 1) + "/" + serverDate.getDate() + "/" +
serverDate.getFullYear() + " " + serverDate.toLocaleTimeString("en-US", { hour12: true });
document.getElementById('<%=SaveTime.ClientID%>').Text = dateString;
}
</script>
My problem is I don't know where to put this javascript function in my apsx page, or how to set the Text value of my label to the date string calculated in the function. I don't even know for sure how to 'call' this function...
So my questions are:
Where do I have to define the javascript function?
How to I 'call' this javascript function when the user clicks the 'Save' button, so that the text one my age is updated?
You can use Java Script Function like
<script type="text/javascript" language="javascript">
function javascriptFunction()
{
}
</script>
call this function
If you Used Update Panels Then You can Use in .Cs Page:
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);
or On Button's Click in .aspx page
<asp:Button ID="Save" runat="server" Text="Save Changes" OnClick="SaveChanges"
onClientClick="javascriptFunction();"
ValidationGroup="ProjectSummaryValidationGroup"
meta:resourcekey="SaveResource1" />
Other Wise You can Use in .cs page
ClientScript.RegisterStartupScript
(GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);
set value of Label in javascript:
document.getElementById('<%=SaveTime.ClientID%>').value = "Your Date";
for label :
void Page_Load(object sender, EventArgs e)
{
lblMyLabel.Attributes.Add("onclick",
"javascript:alert('ALERT ALERT!!!')");
}

RadDatePicker SelectedDateChanged issue

I have RadDatePicker control with SelectedDateChanged event. When I change the Date the event fires with Confirm window. When I click on 'Cancel' button RadDatePicker1_SelectedDateChanged invokes again and it displays Confirm window twice. When I click on 'OK' button it works fine. What could be the problem? Thanks for any suggestions.
<telerik:RadDatePicker ID="RadDatePicker1" runat="server" AutoPostBack="true" OnSelectedDateChanged="RadDatePicker1_SelectedDateChanged" >
</telerik:RadDatePicker>
protected void RadDatePicker1_SelectedDateChanged(object sender, EventArgs e)
{
string radalertscript = "<script language='javascript'>function f(){ radconfirm('Are you sure?', confirmChange, 400, 100) ; Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert33", radalertscript);
}
Here is Javascript function
function confirmChange(args) {
if (args) {
__doPostBack("<%= hiddenButton1.UniqueID %>", "");
}
It seems like an unnessary postback if you're just using it to render some javascript.
Could you do your confirm dialog on the client side?
http://www.telerik.com/help/aspnet-ajax/calendar-on-date-selecting.html
<script type="text/javascript">
function ConfirmChange(sender, eventArgs) {
var date = eventArgs.get_renderDay().get_date();
var dfi = sender.DateTimeFormatInfo;
var formattedDate = dfi.FormatDate(date, dfi.ShortDatePattern);
eventArgs.set_cancel(!confirm("Are you sure you want to " +
(eventArgs.get_isSelecting() ? "select " : "unselect ") +
formattedDate + "?"));
}
</script>
<telerik:RadCalendar ID="RadCalendar1" runat="server">
<ClientEvents OnDateSelecting="ConfirmChange" />
</telerik:RadCalendar>

Refresh GridView from ClientSide (Javascript) in asp.net

I have added the Gridview control on a webPage.
I am deleting any row (one row at a time) by calling PageMethod as follow:
<script type="text/javascript">
function Delete_Row(){
PageMethods.DeleteRow(row_id, GetTimeCallback, ErrorHandler, TimeOutHandler);
}
GetTimeCallback = function (result)
{
if (result) {
alert('Row is deleted');
// I want to refresh the Gridview here
}
}
<script type="text/javascript">
where "row_id" is primery key of the row.
It shows the alert perfectly but does not refresh the Gridview with one less deleted row.
what code should i write to Update the gridview?
NOTE: I dont want to refresh entire page.
Write CallBack Function to acheive this...You can find the Callback Functionality at http://msdn.microsoft.com/en-us/library/ms178208 and http://msdn.microsoft.com/en-us/library/ms178210
Edit:-
protected void Page_Load(object sender, EventArgs e)
{
String cbReference =Page.ClientScript.GetCallbackEventReference(this,
"arg", "ReceiveServerData", "context");
String callbackScript;
callbackScript = "function CallServer(arg, context)" +
"{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
}
System.IO.StringWriter strDataGridHtml= new System.IO.StringWriter();
public void RaiseCallbackEvent(String eventArgument)
{
string idToBeDeleted=eventArgument;
//Write deleteCode
//DataBind the Grid
HtmlTextWriter htwObject = new HtmlTextWriter(strDataGridHtml);
GridViewControl.RenderControl(htwObject);
}
public String GetCallbackResult()
{
return strDataGridHtml.ToString();
}
Now as you see this strDataGridHtml will be sent to Javascript Function ReceiveServerData...
<script type="text/ecmascript">
function ReceiveServerData(rValue)
{
document.getElementById("divIDWhichEncapsulategridView").innerHTML = rValue;
}
</script>
Hope this Will Help you..As i don't i have your full code i can't write the exact one...but this should give you some idea on how to proceed...And also please go through the "CallBack" Functionality in order to understand this functionality to the fullest..

Categories