Open new page using OnClientClick - ASP.Net - c#

I am working in ASP.Net C#. Trying to invoke Downlaod.aspx page to download a file when a button (added in a gridview) is clicked. Following is code.
<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False"
OnClientClick='<%# String.Format("window.open(""../../Views/Common/Download.aspx?PropertyDocumentID={0}""); return false;", Eval("DocumentId").ToString())%>' />
When i click it, i can see following error in browser console.
Uncaught SyntaxError: Unexpected token .
But I am unable to figure out syntax error.

One option would be to make a separate js function and use it like below:
<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick='OpenWindow(<%# Eval("DocumentId").ToString() %>)' />
JS function:
function OpenWindow(documentId)
{
window.open("../../Views/Common/Download.aspx?PropertyDocumentID=" + documentId);
return false;
}

Try Below:
Method 1:
Change double quote to single quote
<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick = '<%#String.Format("window.open('../../Views/Common/Download.aspx?PropertyDocumentID={0}'); return false;');",Eval("DocumentId").ToString()) %>' />
Or remove your string.Format and use like this
<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick = '<%#" window.open('../../Views/Common/Download.aspx?PropertyDocumentID=" + Eval("DocumentId").ToString() + "); return false;" %>' />
Method 2:
HTML
<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick='<%# "LocateMyPage(" + Eval("DocumentId").ToString() + ");" %>' />
Javascript
<script type="text/javascript">
function LocateMyPage(DocID){
window.open('../../Views/Common/Download.aspx?PropertyDocumentID=' + DocID);
return false;
}
</script>

Related

asp.net c# need to redirect to another page with value

I have a page that has a gridview control that lists item info and has options to view the items in detail, and also to open a page to edit the item and save the result as a "clone". This works fine, but now I want to add a button to the detail view page to clone the record without having to return to the list page and select the clone item.
working gridview control on list page:
<asp:GridView ID="GridView1" runat="server" Caption="Submitted Questions" AllowSorting="True"
CaptionAlign="Left" EmptyDataText="You have not submitted any Questions." PageSize="5"
AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#cccccc">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="150" />
<asp:BoundField DataField="SubmitDate" HeaderText="Submitted Date" ItemStyle-Width="50" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" Text="View Details" PostBackUrl='<%#"~/Submit/Detail.aspx?Id=" + Eval("QuestionID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Clone" runat="server" Text="Create Clone" PostBackUrl='<%# "~/Submit/Clone.aspx?Id=" + Eval("QuestionID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Current attempt at the button control:
dropped the javascript below and moved to code behind:
Button:
<asp:Button ID="CloneButton" runat="server" Text="Clone This Question" OnClick="CloneButton_Click" />
Code behind:
protected void CloneButton_Click(object sender, EventArgs e)
{
Response.Redirect("~/Submit/Clone.aspx?Id=" + txt_QuestionID);
}
Now it looks like the app is trying to open the clone page but is having issue with interpreting the value passed for Id?
Error occuring here on the clone page: Error received is "Input string was not in a correct format."
using (Conn)
{
SqlCommand command = new SqlCommand("sp_CloneSElect", Conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("#QuestionID", SqlDbType.BigInt));
command.Parameters["#QuestionID"].Value = Convert.ToInt32(Request["Id"]);
Conn.Open();
SqlDataReader reader = command.ExecuteReader();
Ignore below here as no longer used (saved for reference to notes already submitted).
Javascript:
<script type="text/javascript">
function redirect(QuestionID) { location.href = '~/Submit/Clone.aspx?Id=' + QuestionID; }
</script>
Button:
<asp:Button ID="CloneButton" runat="server" OnClientClick='redirect(<%#Eval("QuestionID") %>; return false' Text="Clone Question" />
Button is not inside of a table, grid, or other control set.
I see a couple of issues.
OnClientClick='redirect(<%#Eval("QuestionID") %>; return false'
has a parentheses missing after >
OnClientClick='redirect(<%#Eval("QuestionID") %>); return false'
Also
location.href = '~/Submit/Clone.aspx?Id=' + QuestionID;
is inside an ordinary html script tag so ~ won't be replaced by server
If <%#Eval("QuestionID") %> returns a string like QUEST3123234 you'd need to add quotation marks. If it's a number, then they are not needed.
Figured it out.
Button control:
<asp:Button ID="CloneButton" runat="server" Text="Clone This Question" OnClick="CloneButton_Click" />
Code Behind:
protected void CloneButton_Click(object sender, EventArgs e)
{
Response.Redirect("~/Submit/Clone.aspx?Id=" + txt_QuestionID.Text);
}
Thanks to all for assisting to jar a newbies brain cells.

Reset ImageButton ImageUrl to default URL

I've an ImageButton into a Repeater.
My ASP.NET code is:
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:ImageButton ID="img_sport" runat="server" CommandName='<%# Eval("IDSport") %>' CommandArgument='<%# Eval("SportName") %>' ImageUrl='<%# Eval("SportName","~/Images/{0}-trp.png") %>' OnCommand="img_sport_Click" />
</ItemTemplate>
</asp:Repeater>
From code behind I change the ImageUrl of selected/clicked image in this way:
[...]
ImageButton btn = (ImageButton)sender;
btn.ImageUrl = "/Images/" + sportName + "-trp-selected.png";
[...]
But, if I click on other images, I'll have two/three/four image shown as selected, so first of all, in my code behind, I would like to reset ImageUrl to default value, then I'll apply the new ImageUrl only to the selected/clicked ImageButton.
So, how can I reset all ImageUrl?
On Seeing your code, I guess you want to change the image when it is clicked so that user can identify that whether image is selected or not. If you want to achieve this, why don't you try JavaScript which can handle same thing without postback.
See below code sample with JavaScript function for the same:
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:ImageButton ID="img_sport" runat="server" ImageUrl='<%# Eval("SportName","~/Images/{0}-trp.png") %>' OnClientClick='<%# "return ManageSelection(this,\"" + Eval("SportName").ToString() + "\");"%>' />
</ItemTemplate>
</asp:Repeater>
<script type="text/javascript">
function ManageSelection(control, sportName) {
var selectedImage = '/Images/' + sportName + "-trp-selected.png";
$(control).attr('src', selectedImage);
return false;
}
</script>

See if bootstrap checkbox is checked asp.net c#

I need to get true or false if bootstrap check-box is checked on server side
This is the control:
<div id="CheckCategoria" runat="server" class="btn-group" data-toggle="buttons-checkbox" runat="server">
<asp:Button ID="ChkInspecao" OnClientClick="return false;" Text="Inspeção" UseSubmitBehavior="False" runat="server" CssClass="btn btn-check" />
<asp:Button ID="ChkHipot" OnClientClick="return false;" Text="Hipot" UseSubmitBehavior="False" runat="server" CssClass="btn btn-check" />
<asp:Button ID="ChkCalibracao" OnClientClick="return false;" Text="Calibração" UseSubmitBehavior="False" runat="server" CssClass="btn btn-check" />
<asp:Button ID="ChkChecagemInterna" OnClientClick="return false;" Text="Checagem Interna" UseSubmitBehavior="False" runat="server" CssClass="btn btn-check" />
<asp:Button ID="ChkRevisao" OnClientClick="return false;" Text="Revisão" UseSubmitBehavior="False" runat="server" CssClass="btn btn-check" />
</div>
as they have runnat="sever", I can get them on server-side but how can I see if it's toggled or not?
I tried:
string Inspecao = ChkInspecao.Attributes["checked"];
but it's returning null.
how can I do that?
You can make beautiful checkboxes with Jquery, please check this page:
ChackBox UI Jquery
Anyway, this is a how:
<script type="text/javascript">
$(function () {
$(".btn").click(function () {
//The old selected gets unselected
$(".Selected").addClass("NoMore");
//This won't
$(this).removeClass("NoMore");
//Because this is the one selected
$(this).addClass("Selected");
//Take the tab selected
$("#ValSel").val($(this).attr("id"));
return false;
});
});
</ script>
<asp:HiddenField ID="ValSel" ClientIDMode="Static" runat="server" />
Then, from code-behind:
Label1.Text = ValSel.Value;
This is the style I used (but it is not important):
<style>
.Selected
{
color:Green;
}
.NoMore
{
color:Red;
}
</ style>
I imagine you would change the background color, but I insist, you can use the checkboxes from the Jquery UI.

Why Eval("ID") doesn't work in the asp:LinkButton?

My codes are
<asp:LinkButton ID="LinkButton1" runat="server"
PostBackUrl='<%# String.Format("~/WebForms/AnaEkran_EPDK.aspx?id={0}", Eval("ID")) %>' >
</asp:LinkButton>
and
<a href="~/WebForms/AnaEkran_Bayi.aspx?id=<%# Eval("ID") %>" >.....</a>
and they don't work. On the linkbutton output is a javascript:__doPostBack('LinkButton1', ''), why doesn't id take?
go for OnClientClick of link button
from code behind
put this in page load
LinkButton1.OnClientClick = "window.location = '~/WebForms/AnaEkran_EPDK.aspx?id="+ ID.toString() + "'; return false;"
Or in aspx do this
<asp:LinkButton ID="LinkButton1" runat="server"
OnClientClick="goToYourPage();" >
</asp:LinkButton>
Try adding this attribute to your LinkButton:
href='<%#"~/WebForms/AnaEkran_Bayi.aspx?id="+Eval("ID")%>'

Two popups on a single button with a condition

I was needing a popup that should be decided with a javascript function. I do not get a popup when i was using the below function. I think am doing a mistake somewhere. Could someone point out what it is?
The save button calls the javascript function
<asp:ImageButton ID="btnSave" runat="server" CausesValidation="true" OnClientClick="isPageValid();return false;" ImageUrl="~/images/green-save.gif"
OnClick="btnSave_Click" TabIndex="22" ValidationGroup="groupProfile" /></td>
function isPageValid()
{
var validated = Page_ClientValidate('groupProfile');
var loccount = document.getElementById('txthiddenloccount').value;
if(validated)
{
if(loccount = "1")
{
var mdlPopup = $find('<%= ModalPopupExtendersavechanges.ClientID %>');
}
else
{
var mdlPopup = $find('<%= ModalPopupExtenderMerchantUpdate.ClientID %>');
}
if(mdlPopup)
{
mdlPopup.show();
}
}
}
<cc1:ConfirmButtonExtender DisplayModalPopupID="ModalPopupExtenderMerchantUpdate" ID="ConfirmButtonExtenderMerchantUpdate"
OnClientCancel="ManageCancel()" runat="server" TargetControlID="btnHidden">
</cc1:ConfirmButtonExtender>
<cc1:ModalPopupExtender ID="ModalPopupExtenderMerchantUpdate" runat="server" BackgroundCssClass="modalBackground"
CancelControlID="btnCancel" PopupControlID="pnlPopupMerchantUpdate" TargetControlID="btnHidden">
</cc1:ModalPopupExtender>
<asp:Button ID="btnYesMerchant" Text ="Yes" runat="server" class="popupButton" causesvalidation="true" OnClientClick="$find('mdlpop').hide(); return true;" onclick="btnYessave_Click"/>
<asp:Button ID = "btnNoMerchant" Text ="No" runat ="server" class="popupButton" causesvalidation="true" OnClientClick="$find('mdlpop').hide(); return true;" onclick="btnNosave_Click"/>
<asp:Button Id="btnCancel" Text ="Cancel" runat="server" class="popupButton" />
And the second is
<cc1:ConfirmButtonExtender DisplayModalPopupID="ModalPopupExtendersavechanges" ID="ConfirmButtonExtendersavechanges"
OnClientCancel="ManageCancel()" runat="server" TargetControlID="btnHidden">
</cc1:ConfirmButtonExtender>
<cc1:ModalPopupExtender ID="ModalPopupExtendersavechanges" runat="server" BackgroundCssClass="modalBackground"
CancelControlID="btnNo" OkControlID="btnYes" PopupControlID="pnlPopupsaveChanges" TargetControlID="btnHidden">
</cc1:ModalPopupExtender>
<asp:Button ID="btnYes" Text ="YES" runat="server" class="popupButton" causesvalidation="true" onclick="btnSave_Click"/>
<asp:Button Id="btnNo" Text ="NO" runat="server" class="popupButton" />
if(loccount = "1")
Looks like we have a problem with the equals sign captain. = != ==
You actually want to show the behavior of the ModalPopupExtender, not the extender itself. Try adding a BehaviorID attribute to each ModalPopupExtender, then use the following JQuery code:
if(loccount == "1")
{
var mdlPopup = $find('ModalPopupExtenderSaveChangesBehaviorID');
}
else
{
var mdlPopup = $find('ModalPopupExtenderMerchantUpdateBehaviorID');
}
if(mdlPopup)
{
mdlPopup.show();
}

Categories

Resources