Link OnClick code behind does not execute when set as ModalPopupExtender TargetControlID - c#

I did a lot of searching and cannot figure this out.
I have a ModalPopupExtender pop-up that I want to display when the user clicks a link DoSomething. The pop-up has a dropdown control that I then want to populate on the fly when the user asks to open the dialogue. This needs to happen server side via the code behind. Currently I am trying to do it via an OnClick event on the link but as soon as the link is tied to the ModalPopupExtender the link OnClick code is not executed.
Code snippet:
<asp:LinkButton ID="lnkDoSomething" runat="server" onClick="lnkDoSomething_Click">Do Something</asp:LinkButton>
<asp:ModalPopupExtender ID="mpelnklnkDoSomething" runat="server" BackgroundCssClass="modalBackground"
DropShadow="true" PopupControlID="lnkDoSomething"
PopupDragHandleControlID="pnlDragHandlerForlnkDoSomething"
TargetControlID="lnklnkDoSomething"></asp:ModalPopupExtender>
The problem is as soon as I set the ModalPopupExtender to the link the OnClick code does not execute. This obviously is by design but it doesn't make sense to me (naive) as if the user clicks the link the OnClick code should be executed.
Any ideas why this is not supported and what the correct solution is?

Attach the ModalPopupExtender to a dummy button and show the modal on the LinkButton's OnClick even from the code-behind:
Markup:
<asp:LinkButton ID="lnkDoSomething" runat="server" onClick="lnkDoSomething_Click">Do Something</asp:LinkButton>
<asp:Button id="dummyButton" runat="server" style="display:none;" />
<asp:ModalPopupExtender ID="mpelnklnkDoSomething" runat="server"
BackgroundCssClass="modalBackground" DropShadow="true" PopupControlID="controlToPopUpId"
PopupDragHandleControlID="pnlDragHandlerForlnkDoSomething"
TargetControlID="dummyButton"></asp:ModalPopupExtender>
Code-behind:
protected void lnkDoSomething_Click(Object sender, EventArgs e)
{
//do work
mpelnklnkDoSomething.Show();
}

Related

asp.net search button just refreshing page

Im trying to create a search button on my website on my homepage which is an aspx page but when I click search all it does is refresh the page instead of preforming the query, any help would be appreciated
heres the code for my index.aspx and the code for my index.aspx.cs page
<asp:TextBox ID="searchtitle" runat="server"></asp:TextBox>
<asp:Button ID="searchitems" runat="server" Text="Search" />
protected void searchitems_Click(object sender, EventArgs e)
{
String stext = searchtitle.Text;
Response.Redirect("search.aspx?searchquery=" + stext);
}
The button is not working because you didnt call the event on the button
<asp:Button ID="searchitems" runat="server" OnClick="searchitems_Click" Text="Search" />
add OnClick attribute with name of the event you want to call and in the given code the event you want to call is searchitems_Click.
definitely when you will click the searchitems page will reload because function for that click is defined on server side.. what you have to do is on the pageload look for the QueryString and get the value of ["searchquery"] then manipulate it as you want..
if you want to not refresh the page use Ajax then...

Event in repeater not fired until page postback by another control in asp.net c#!

Why My Event as link button event or item command not fired until i post back page with another control in page.
for example in this code:
<div id="pagination">
<span class="all" runat="server" id="CurrentPage">Total Pages</span>
<asp:Repeater ID="RPTPaging" runat="server" Visible="false" OnItemDataBound="RPTPaging_ItemDataBound" OnItemCommand="RPTPaging_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="BtnPage" CssClass="inactive" CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" Text="<%# Container.DataItem %>"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>
related to this question
Why Repeater ItemCommand Doesn't fire, even if i don't rebind by post back?
i found when i click on link button in this repeater nothing happened,but after i click on empty input button on page, after post back a page changed corectlly.
Dear Expert After 2 day finally i figure out why my page doesn't post back.
after i tried to post back my button manually with java script and i failed. i tried to trace the script and i found this Error with firebug:
TypeError: theForm.submit is not a function
theForm.submit();
when i google it i found i have a button in my MasterPage with id="Submit" and as
"submit is not a function" means that you named your submit button or some other element submit. Rename the button to btnSubmit and your call will magically work.
When you name the button submit, you override the submit() function on the form.
So This Problem Resolved. Thanks to all.

How can I identify, in an ASP.NET Page_Load event, what RadButton initiated a postback?

In my ASP.NET page's Page_Load I'm trying to determine whether a certain button has been clicked and is attempting a postback:
if (Page.IsPostBack)
{
if (Request.Params.Get("__EVENTARGUMENT") == "doStuff")
doSomething();
}
doStuff is a JavaScript within the markup, but I don't want this alone to trigger the doSomething() method call, I also need to be able to ensure that the button the user has clicked is correct.
How can I identify and reference the button control from the code behind once the user clicks? I searched for and discovered this but when I try to implement it, the control returned is always null.
Or use the CommandName and command events.
<telerik:RadButton ID="RadButton1" runat="server" CommandName="first" CommandArgument="one" OnCommand="CommandHandler" />
<telerik:RadButton ID="RadButton2" runat="server" CommandName="second" CommandArgument="two" OnCommand="CommandHandler" />
<asp:Label ID="Label1" Text="" runat="server" />
<asp:Label ID="Label2" Text="" runat="server" />
protected void CommandHandler(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandArgument.ToString();
Label2.Text = e.CommandName;
}
You haven't really explained what you're trying to do, but it sounds like you would have a much easier time if you added an OnClick event handler to the button instead of trying to figure it out in Page_Load. The event handler would only be executed when that button is clicked.

Button need to be click twice to fire an event

I have a simple ASP.NET Button inside of iFrame, for strange reason i have to click it twice in order to fire an event.
<asp:Button ID="btnSaveComment" runat="server" Text="Add Comment"
CssClass="button"
OnClick="btnSaveComment_Click"
ValidationGroup="add" />
protected void btnSaveComment_Click(object sender, EventArgs e)
{
AddComment();
}
I suspect that when you initially click an area above the button you're giving focus to the iframe (that is to say, the page within it), and on the second click the mouse can interact with the button.
On the other hand, if the page seems to be posting back but not actually doing anything until the second click, then it might well be related to the page lifecycle, as suggested in an answer comments by #AndroidHustle.
Edit:
To test the theory of whether or not the frame is focused, try giving it focus via script. Something like the following might help:
<script type="text/javascript">
document.getElementById("iFrameId").focus();
</script>
I had the same problem. Here is how I solved it:
$("#MainContent_Button1").hover(function (event) {
$("#MainContent_Button1").click()
});
Now it only takes 1 click, and the button functions like it's supposed to.
<asp:Button ID="Button1" runat="server" Text="Save Payment Plan" OnClick="Button1_Click" />

How close Html window when click asp.net button?

I have asp.net button "OK" in html popup window. I after my logic done how close that popup window it self?
<asp:Button Id="btnOK" runat="server" AccessKey="<%$Resources:
wss,multipages_okbutton_accesskey%>" Width="70px" Text="<%$Resources:wss,
multipages_okbutton_text%>" OnClick="btnOK_Click" />
<asp:Button ID="btnOK" runat="server" OnClientClick="window.close(); return false;" Text="Close" />
All correct but there is another way if you want close the window in your code:
Suppose that the button ID is "ContineButton" and the click event handler name is "ContineButton_Click"
protected void ContineButton_Click(object sender, EventArgs e)
{
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", "window.close()", true);
}
If there is a chance that your server side code may fail, and you need to keep the popup open to correct errors, the OnClientClick trick won't help. I do this with a PlaceHolder and a small script:
<asp:PlaceHolder id="close_script" runat="server">
<script>window.close();</script>
</asp:PlaceHolder>
Then, in the button handler, set the Visible property of the PlaceHolder to close the popup (or leave it open:
protected void btnOK_Click(Object sender, EventArgs e) {
bool success = processPage();
close_script.Visible = success;
}
That requires some javascript. Change your button markup to this:
<asp:Button Id="btnOK" runat="server" AccessKey="<%$Resources:
wss,multipages_okbutton_accesskey%>" Width="70px" Text="<%$Resources:wss,
multipages_okbutton_text%>" OnClick="btnOK_Click" OnClientClick="javascript:window.close(); return false;" />
There are other things to consider here - an accessible site will work without JavaScript including opening and closing a popup window. It will be dumbed down, but still function. Take a look at this article:
http://accessify.com/features/tutorials/the-perfect-popup/

Categories

Resources