Calling OnClientClick from Code behind in Asp.net VB - javascript

I have code like this :
<asp:LinkButton runat="server" ID="HL_KonfirmasiPemesanan" class="button-primary button-shadow" causesvalidation="false" validationgroup="ValidasiData" OnClientClick="return getConfirmation(this);" ></asp:LinkButton>
Then I want call OnClientClick="return getConfirmation(this);" in code behind, I tried:
HL_KonfirmasiPemesanan.Attributes.Add("OnClientClick", "return getConfirmation(this);")
but it doesn't work, how can I do that?

You can do this:
dim script as String = "<script>var btn = document.querySelector('.button-primary'); getConfirmation(btn);</script>"
ClientScript.RegisterStartupScript(Me.GetType(), "click_Btn", script)

Related

OnClIck in Javascript Not Working For Image

I have OnClick Function and On ClientClick Function as
<asp:ImageButton ID="imgbtnSchedule" runat="server" ClientIDMode="Static" ImageUrl="~/images/Update2.png" ToolTip="Refresh" OnClick="imgbtnSchedule_Click" OnClientClick="return validateUpdate(1);" />
My OnClick Function Is Not Working so I Started Debugging using F12. but Here I can see my OnClick function Is Not there and Onclientclick property is showing as OnClick.
I have Onlick Function On Server side and An OnClientClick Function On
client side,I am expecting Ececution Of both OnClick and
OnClientClick,How To Achive this
base on that code
<asp:ImageButton ID="imgbtnSchedule" runat="server" ClientIDMode="Static" ImageUrl="~/images/Update2.png" ToolTip="Refresh"
OnClick="imgbtnSchedule_Click"
OnClientClick="return validateUpdate(1);" />
If the validateUpdate(1) returns false, then the imgbtnSchedule_Click will not called.

ASPx.Net How to catch a change in value in an Itemtemplate DDL

I'm struggling to find a way to solve this.
I have a gridview and the first column of it is a DropDownList defined in an Itemtemplate:
<asp:GridView ID="gvXYZ" runat="server" DataKeyNames="Serial, XYZValue">
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="ddlStat" runat="server" OnSelectedIndexChanged="ddlStat_SelectedIndexChanged"><asp:ListItem> </asp:ListItem><asp:ListItem> </asp:ListItem><asp:ListItem>OK</asp:ListItem><asp:ListItem>NG</asp:ListItem></asp:DropDownList>
</ItemTemplate>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
</asp:TemplateField>
</Columns>
</asp:GridView>
The user is presented with an empty choice, OK or NG as possible selections.
How can I trigger either JavaScript or VB side to run a function when the user makes any selection in any of the DDL in the grid?
In order to complete what asked, the user has to have either OK or NG selected.
I'm trying to tie the Save button to the fact that the grid has been completed.
I know I can run JavaScript on a HTML dropdown so I tried to create a function to do it and from an article I found I tried to run a VB method from a javascript function:
function ddlStat_SelectedIndexChanged() {
var someValueToPass = 'Hello server';
__doPostBack('CustomPostBack', someValueToPass);
}
the script never runs, the postback does not occur and the VB side code:
Protected Sub ddlStat_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim x As Integer
For x = 1 To 10 : x = x + 1 : Next
End Sub
is never triggered either.
I thought to do this on the VB side because I think I can more easily access properties of the grid, like the number of rows so I can check the DDL in each one of them.
Thank you for this and ask questions if I wasn't clear.
Setting AutoPostback property of the drop down should raise a post back call to the server side. JS function can be called using the HTML event onchange
<asp:DropDownList ID="MyDropDown" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="SelectedChange" onchange="YourChangeFun(this);">
</asp:DropDownList>
Javascript:
<script type="text/javascript">
function YourChangeFun(ddl)
{
alert(ddl.selectedIndex);
}
</script>

Passing variables to javascript in repeater imagebutton onclientclick

I want to pass my dynamic value to a javascript function from an imagebutton onclientclick event.
It works fine if I pass the static value like this
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('1')" />
However it doesn't work if I try to pass dynamic values as below.
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent" BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('"+<%#Eval("PujaType") %>+"')" />
I tried all possible combinations however it says tag is not well formatted.
Can anyone help on this?
Don't know ASP, but in JSP the expression has to be the entire value. Try:
OnClientClick="<%# "javascript:return openWindow('" + Eval("PujaType") + "')" %>" />
You don't need string concatenation around your Eval statement. Doing so is creating invalid markup.
Change
"javascript:return openWindow('"+<%#Eval("PujaType") %>+"')"
to
"javascript:return openWindow('<%#Eval("PujaType") %>')"
<asp:ImageButton
ImageUrl="~/Images/control_play_blue.png"ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('<%#Eval("PujaType") %>')" />
Its been awhile since I have worked with ASP.NET and currently cannot test this, but I seem to recall it requiring single quotes when wrapping the <%# %>.
Something like this:
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick='<%# "javascript:return openWindow('" + Eval("PujaType") + "')" %>' />
Some of the answers on this question suggest the same.
An alternative would be setting the value in the code behind. Assuming you are using this control inside a repeator or something similar, you should be able to set the value during data binding like this:
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
String pujaType = ((YourType)e.Item.DataItem).PujaType;
((ImageButton)e.Item.FindControl("btnChat")).OnClientClick = "javascript:return openWindow('" + pujaType + "')";
}
}
Repeater.ItemDataBound Event
Another alternative, since you are not making use of any serverside events, would be to use regular html instead of an ASP.NET control. This could be done with an anchor and img tag like so:
<a href="#" onclick="openWindow('<%# Eval("pujaType") %>'); return false;">
<img src='<%= ResolveUrl("~/Images/control_play_blue.png") %>' />
</a>

Populating existing dropdownlist using AsyncPostBackTrigger, not populate a new dropdownlist

For my web app, I tried to populate dropdownlist (DDL) based on what client click on the calendar extender ( I have this declared in codebehind vb.net to load it from database) using AsyncPostBackTrigger (because I dont want the page to autopostback). I have the button to fire the date selected and it did worked to populate the DDL except not really the way I wanted it to. Instead of updating it in the existing DDL, it creates a new DDL besides the existing ones. I tried looking for solution but did not managed to find any. Anyone can help me figure out why this happens and how to fix this?
Here's my asp.net & javascript code
<asp:Label ID="label16" runat = "server" Text="Select the date"></asp:Label></td>
<td id="Td29" style="width:290px;" runat="server">
<asp:TextBox ID="tbPostingDt" runat="server" style ="width:174px;"></asp:TextBox>
<asp:Button ID="ClickBtn" runat="server" Text="Click" style="display:none" OnClick="ClickBtn_Click" />
<asp:ImageButton ID="CalendarBtnPostingDt" runat="server" ImageUrl="~/Images/Calendar_scheduleHS.png" AlternateText="Click to display calendar"/>
<cc1:CalendarExtender ID="calPost" runat="server" PopupButtonID="CalendarBtnPostingDt"
TargetControlID="tbPostingDt" OnClientDateSelectionChanged="dateSelectionChanged" Format="dd-MMM-yyyy" Enabled="True"></cc1:CalendarExtender></td>
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="label18" runat = "server" Text="Post Cycle No"></asp:Label></td>
<td id="Td33" style="width:290px;" runat="server">
<asp:DropDownList ID = "ddlPostCycleNo" runat = "server" style ="width:180px;">
<asp:ListItem>ALL</asp:ListItem>
</asp:DropDownList> </td>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ClickBtn" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
function dateSelectionChanged(sender, args) {
var PostingDt = document.getElementById('<%= tbPostingDt.ClientID%>').value.trim();
var clickButton = document.getElementById("<%= ClickBtn.ClientID %>");
clickButton.click();
}
nevermind I figured it out already. I used the ontextChanged for the textbox when user select the date from calendar, and removed OnClientDateSelectionChanged, triggers and dummy button. I keep the UpdatePanel and it works just the way I wanted.

Passing Repeater Container.ItemIndex to a Javascript function

In C# Asp.Net I need to pass my repeater occurrence index into a Javascript function when OnClientClick is depressed from an ASP button. Here is my code
<asp:Button ID="btnGetNewQuote"
Text="Get New Quote"
class="pcbutton"
runat="server"
OnCommand="GetNewQuote"
CommandArgument ='<%# Container.ItemIndex %>'
OnClientClick="return getNewQuote(<%# Container.ItemIndex %>) />
If I hard code ....OnClientClick="return getNewQuote(0)"> it works and the JS gets invoked, but as soon as I put the # Container.ItemIndex # in there the JS gets overlooked and it just posts back to the code behind...
Passing the Container.ItemIndex into JS Functions works every where else on the page except this OnClientClick ?
does anyone know why this is and what I can do to get around it?
Try this
OnClientClick='<%# "return getNewQuote(" + Container.ItemIndex + ")" %>'
Instead of
OnClientClick="return getNewQuote(<%# Container.ItemIndex %>)
If we want to use in vb.net
OnClientClick='<%# "return getNewQuote(" & Container.ItemIndex & ")" %>'
otherwise it will give exception string is not in correct format

Categories