I need to call 2 functions in the onclick using the LinkButton control, it cannot execute the javascript Function :
<asp:LinkButton ID="btnVirement" value="virement" runat="server" style="color: #f15d22;" onclick="CatchLinkVirement();btnVirement_Click" ><u><b>Comment effectuer un virement ?</b></u></asp:LinkButton>
and this is the CatchLinkVirement() javaScript function :
function CatchLinkVirement() {
var pLinkVirement = document.getElementById("btnVirement").value;
sessionStorage.setItem("pClickVirement", pLinkVirement);
alert(pLinkVirement);
}
and this is my codebehind :
public void btnVirement_Click(object sender, EventArgs e)
{
HttpContext.Current.Session["BonSavoirPopup"] = "BonAsavoirVirement";
Response.Redirect("Mytransfers.aspx");
}
You can try like this:
OnClick="btnVirement_Click" OnClientClick="return CatchLinkVirement();"
Write below line inside your button click handler(Server Side)
ScriptManager.RegisterStartupScript(this, this.GetType(), "SimpleScript", "CatchLinkVirement();", true)
This way you can call javascript function from code behind.
Try this,
<asp:LinkButton ID="btnVirement" value="virement" runat="server" OnClick="btnVirement_Click" OnClientClick="return CatchLinkVirement();">Comment effectuer un virement ?</asp:LinkButton>
in above code hierarchy of function calling is,
OnClientClick JavaScript function ie. CatchLinkVirement called, then
OnClick server event(function) ie. btnVirement_Click called
On single click on LinkButton both function called. OnClientClick event was works only on server control.
Try this.If you want to call code behind from Javascript
<asp:LinkButton ID="btnVirement" value="virement" runat="server" style="color: #f15d22;" onclick="btnVirement_Click" OnClientClick="return CatchLinkVirement(); ><u><b>Comment effectuer un virement ?</b></u></asp:LinkButton>
JavaScript Function
function CatchLinkVirement() {
var pLinkVirement = document.getElementById("btnVirement").value;
sessionStorage.setItem("pClickVirement", pLinkVirement);
alert(pLinkVirement);
document.getElementById('btnVirement').click();
}
Code .aspx.cs
public void btnVirement_Click(object sender, EventArgs e)
{
HttpContext.Current.Session["BonSavoirPopup"] = "BonAsavoirVirement";
Response.Redirect("Mytransfers.aspx");
}
Related
I'm trying to upload a file onchange event of the "Fileupload" control inside gridview.
Means when ever user uploads the file, there itself I needs to save the file content in DB.
So, I had mannually called the click event of the button control on the change of fileupload control But its throwing as like exception like "Invalid postback or callback argument...."
my gridview code :
<asp:GridView runat="server" ID="grd" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="Student ID" />
<asp:BoundField DataField="StudentName" HeaderText="Name" />
<asp:TemplateField HeaderText="Upload">
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" EnableViewState="true" onChange="FileUploadCall(this)" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="Upload" Style="display: none" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
My script Code :
<script type="text/javascript">
function FileUploadCall(fileUpload) {
if (fileUpload.value != '') {
var a = $('#<%=grd.ClientID %>').find('[id*="btnUpload"]');
a.click();
}
}
</script>
My Hidden Button mannual click creation in cs file :
protected void Upload(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow gvr = (GridViewRow)btn.Parent.Parent;
FileUpload lbleno = (FileUpload)gvr.FindControl("FileUpload1");
lbleno.SaveAs(Server.MapPath("~/Uploads/" + Path.GetFileName(lbleno.FileName)));
//lblMessage.Visible = true;
}
Your jquery code that gets the button to upload may be the reason.
Since you said you are using a gridview, so there could be multiple rows each having its own fileupload and button controls. You need to get the button control associated with this row in grid view. To get the associated button, you should be using the jquery code like below, since the associated button immediately follows the fileupload control.
if (fileUpload.value != '') {
var a = $(fileUpload).next("[id*='Button1']");
a.click();
}
The easiest way is to assign the onchange event from code behind so you can get the correct button easily. So create a RowDataBound event for the GridView.
<asp:GridView ID="grd" runat="server" OnRowDataBound="grd_RowDataBound">
Then in the RowDataBound method, use FindControl to locate and cast the FileUpload and the Button. In the method you can assign the change event to trigger a PostBack of the corresponding button.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//use findcontrol to locate the controls in the row and cast them
Button btn = e.Row.FindControl("btnUpload") as Button;
FileUpload fu = e.Row.FindControl("FileUpload1") as FileUpload;
//assign the button postback to the change of the fileupload
fu.Attributes.Add("onchange", "__doPostBack('" + btn.UniqueID + "','')");
}
}
Your implementations is fine only change:
a.click(); => a[0].click(); //important!!
and I hope no binding is happening in the postback:
if (!IsPostBack)
{
var list = new List<Student>();
list.Add(new Student() {StudentID = 1, StudentName = "111"});
list.Add(new Student() {StudentID = 2, StudentName = "222"});
grd.DataSource = list;
grd.DataBind();
}
I've tested it works totally fine!
I have a Listbox defined as:
<asp:ListBox ID="lst_Agenda" onchange="drawChart()" OnSelectedIndexChanged="lst_Agenda_SelectedIndexChanged" SelectionMode="Multiple" CssClass="txt12 myListBox" AutoPostBack="true" DataTextField="Name" runat="server" Width="100%" Height="100%" />
After we select some items in this, lst_Agenda_SelectedIndexChanged method of codebehind is called which calls some other ListBoxes to be populated in cascading way. There is a function in Javascript named as drawChart which generates a Chart based on values selected in all these ListBoxes.
Hence I want that codebehind function should be called before the javascript function call on listbox changed event. But currently it is happening just opposite, Javascript function drawChart gets called first without filling values in the listboxes.
I tried calling Javascript function from codebehind method directly by using:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "myscript", "drawChart()", true);
But using this gives me null values for any 'document.getElementById' call.
The Codebehind Function and JS call is as:
protected void lst_Agenda_SelectedIndexChanged(object sender, EventArgs e)
{
lst_Agenda_Changed();
foreach (ListItem li in lst_SubAgenda.Items)
li.Selected = true;
foreach (ListItem li in lst_Doctors.Items)
li.Selected = true;
lst_SubAgenda_Changed();
lst_Group_Changed();
//ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "myscript", "drawChart()", true);
}
The Javascript for drawChart is as:
function drawChart() {
var tempDateToWeek = "";
var tempDateToYear = "";
var DateOfWeek = new Date();
tempDateToWeek = document.getElementById('txtRangeTo').value.substring(0, 2);
tempDateToYear = document.getElementById('txtRangeTo').value.substring(6, 10);
...........
I am getting error in drawChart if I call it from codebehind at line where I fill variable tempDateToWeek by value of document.getElementById('txtRangeTo').
So how can I achieve this?
You should call RegisterStartupScript instead of RegisterClientScriptBlock in your event handler in code-behind. It will cause the Javascript code to be executed once the page has loaded after the postback, so that the DOM elements will be accessible with document.getElementById.
protected void lst_Agenda_SelectedIndexChanged(object sender, EventArgs e)
{
lst_Agenda_Changed();
foreach (ListItem li in lst_SubAgenda.Items)
li.Selected = true;
foreach (ListItem li in lst_Doctors.Items)
li.Selected = true;
lst_SubAgenda_Changed();
lst_Group_Changed();
ScriptManager.RegisterStartupScript(this, GetType(), "DrawChart", "drawChart();", true);
}
The onchange event handler should also be removed from the DropDownList to avoid calling the client code before the postback.
Why don't you try removing onchange="drawChart()" from asp Control.
Hope this will help you.
//your code here
string script = "window.onload = function() { drawChart(); };";
ScriptManager.RegisterClientScriptBlock(this, this.Page.GetType(), "drawChart", script,true);
And
tempDateToWeek = document.getElementById("<%=txtRangeTo.ClientID %>").value.substring(0, 2);
script(update value)
$("#txtPlanDate").datepicker();
design page
<asp:TextBox ID="txtPlanDate" ReadOnly="true" runat="server" Text='<%# Bind("PlanDate", "{0:dd/MM/yyyy}") %>'></asp:TextBox>
code behind (get value)
protected void GridView1_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{TextBox txtPlanDate = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].FindControl("txtPlanDate");}
I suppose you have textbox inside gridview templatefield . Then You need something like this
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtPlanDate = (e.Row.FindControl("txtPlanDate") as TextBox);
}
It works OnRowDatabound event.Not tested OnRoUpdating event but it should work fine.
I have a button with OnClick event on my web page.
<asp:TextBox ID="tbMailID" runat="server" Width="250px"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
C# code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool IsGmail = CheckMailId(tbMailID.Text);
if(!IsGmail)
{
string strScript = "confirm('Mail id is not from gmail, do you still want to continue?');";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "strScript", strScript, true);
}
SendMail();
}
The page accepts a mail id in textbox and on submit sends a mail.
But if mail-id is not an gmail id (validated by CheckMailId) i want to show a confirmation box to user whether he/she wants to get a mail, based on the Yes/No clicked.
But the javascript will get call after the submit event is served and mail is sent.
if i add a return SendMail() will never get calls
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "strScript", strScript, true);
return;
What could be a possible solution here?
Note: the code posted is simplified version of my real use case.
CheckMailId - is just a name to show, it calls up many other functions and do few db process. So incorporating that in javascript, i would like to avoid.
You can call a script befroe event lke this:-
<script type="text/javascript">
function Confirm() {
var yourstring = document.getElementById('<%= tbMailID.ClientID %>').value;
if (/#gmail\.com$/.test(yourstring)) {
return true;
}
else
{
if (confirm("Mail id is not from gmail, do you still want to continue?") == true)
return true;
else
return false;
}
}
</script>
strong text
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return Confirm();" OnClick="btnSubmit_Click" Text="Submit" />
Good day,
I have a repeater that contain link button. The value and number of link button is generate base on data pull from database. The HTML code is something as follow:
<asp:Repeater ID="repCategories" runat="server" EnableViewState="false">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Here is some code that I try to do in my code behind,
for (int i = 0; i < table.Rows.Count; i++)
{
RepeaterItem itm = repCategories.Items[i];
GiftRow dr = tbl.GetRow(i);
Literal litLink2 = (Literal)itm.FindControl("litLink2");
litLink2.Text = dr.Name;
string myScript = string.Empty;
myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
myScript += "alert('hi');";
myScript += "\n\n </script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
}
By doing this, I will get alert hi when I load the page.What I want to do is, I only want it do alert hi when I click on the link button, instead of page load.
Use a LinkButton and leverage the ClientClick event instead:
<asp:Repeater ID="repCategories" runat="server" EnableViewState="false">
<ItemTemplate>
<asp:LinkButton ID="lbMyLinkButton" runat="server" />
</ItemTemplate>
</asp:Repeater>
I'd also recommend you use ItemDataBound events of repeaters where possible:
void repCategories_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
((LinkButton)e.Item.FindControl("lbMyLinkButton")).OnClientClick = "alert('hi');";
}
}
NOTES:
I wouldn't ever recommend you render javascript server-side unless absolutely necessary. I've provided an answer to fit in with the context of your question, but I would advise you have a javascript function already on your page and 'wire it up' server-side (if not bind it on the client with pure javascript)
There is also an assumption here that you're binding data to the repeater (you should be anyway), which means you also have to wire up the ItemDataBound handler: repCategories.OnItemDataBound += repCategories_ItemDataBound