Keep jQuery modal open on C# button click - c#

I have a modal on a page that was suposed to remain open when I click a link on a ASP.NET GridView row. The click of this link fires the click of another button that calls a function on server-side. My idea was to keep my modal open, so I could fill it with what I need, using code behind. The problem is that after the modal is opened and the button is fired, the modal closes (if I remove the button click, everything works fine). I would like to know if it's possible to make it remain open. Here's what I have:
The function that open the modal and fire the button:
function showDetails()
{
$("#modalDetails").dialog("open");
var btnEmbedDetails = document.getElementById('<%=btnEmbedDetails.ClientID %>');
btnShowDetails.click();
}
The link inside the GridView row:
<asp:TemplateField ItemStyle-Width="100px" HeaderText="Details">
<ItemTemplate>
<a href='javascript:showDetails();'>Open it</a>
</ItemTemplate>
</asp:TemplateField>
The button that calls the function on code behind:
<asp:Button ID="btnEmbedDetails" runat="server" OnClick="EmbedDetails" style="display:none;" />
The function on code behind:
public void EmbedDetails(object sender, EventArgs e)
{
string ok = "ok";
}

Related

OnClick event of button fires everytime on PageLoad

I have a button below:
<asp:Button ID="btnApprove" runat="server" Text="Approve" CssClass="button" OnClick="btnApprove_Click" />
Event handler of this button on server side is :
protected void btnApprove_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "key", "alert('Button Approve Clicked')", true);
}
Just get alert on the button click from the server side.
My issue is that once I clicked on Approve button, now when I load or refresh my page again this btnApprove_Click event gets executed everytime.
I have many others button on the same form but none shows this strange kind of behaviour. I tried to change this button as HTML but still the same behaviour.
Can anyone please help me to get out of it. Thanks in advance.
You should use IsPostBack on Page load to prevent every time page load on button click. You can check every time on Page_Load.
Sample Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// First Time Load When User Come
}
else
{
// Every Time Load When Any click button in page
}
Try to Add OnClientClick="return false;"
<asp:Button ID="btnApprove" runat="server" Text="Approve" CssClass="button" OnClick="btnApprove_Click" OnClientClick="return false;"/>
How about using RegisterOnSubmitStatement instead?
Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "key", "alert('Button Approve Clicked')");

asp.net submit button inside update panel and fileupload controls

I have 3 file upload controls where people upload csv files. If my submit button isn't inside an update panel the file upload have the files in the code behind and everything works fine. However, I would like my submit button inside an update panel as based on some other controls on the form I enable/disable the button via the update panel. This enabling and disabling works fine, but now in the button click codebehind the file upload controls always have null values even though a csv file was selected.
Why is the update panel around my submit button causing the file upload controls to not have anything in them even though files were selected?
So I have a FileUpload control on the page like:
<asp:FileUpload ID="file1" runat="server" />
I have a submit button in an update panel like:
<asp:UpdatePanel ID="pnlSubmitButton" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="button1" runat="server" Test="Submit" onclick="button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
In my code behind on the button click event
protected void button1_Click(object sender, EventArgs e)
{
// file1.FileName is empty even though I did select a file
}
If I remove the update panel around the button file1.FileName in the button click is then populated
As far as I know, asp:FileUpload wont works under UpdatePanel async trigger. You need to set upload button click under post back trigger to make it working. Something like this:
<triggers>
<postbacktrigger controlid="btnSubmit"/>
</triggers>

Page losing formatting

On my page a I have a few textboxes, AJAX tabpanel and a gridview.
In the Page_Load event, textboxes are filled in; gridview is populated and so on.
My gridview has a button:
<Columns>
<ItemTemplate>
<asp:Button ID="btnRedirect"
Text="Click me"
CommandArgument='<%#Eval("BkId_ZW")%>'
OnClick="DoRedirect"
runat="server" />
</ItemTemplate>
...
</Columns>
and the code behind it looks like:
protected void DoRedirect(object sender, EventArgs e)
{
Button theButton = sender as Button;
string url ="http://../profile/" + theButton.CommandArgument;
Response.Write("<script>window.open('" + url + "');</script>");
}
After button is pressed a new window is open (everything works as expected) but the main page loose values and formatting of the textboxes.
What is going on? How to fix it?
Response.Write() is not favored for this purpose. Since you are adding to the response in the middle of the ASP.net page lifecycle, you are changing the page output. If you view source, you might find your <script>window.open...</script> line in some awkward place.
Therefore, use Page.ClientScript.RegisterStartUpScript() instead.

Session Variable in ASP.NET returns previous value in ModalBox?

I have a question regarding passing Session Variables to a text-box in an Update Panel (which is displayed in a Modal PopUp).
This is the code I have so far:
ASPX CODE:
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Link" runat="server" OnClick="LinkButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="panel_Load">
<ContentTemplate>
<asp:Button ID="OKButton" runat="server" Text="Close" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="ClientButton" PopupControlID="UpdatePanel1" OkControlID="OKButton">
</asp:ModalPopupExtender>
<asp:Button ID="ClientButton" runat="server" Text="Launch Modal Popup (Client)" style="display:none;" />
CODE BEHIND (C#):
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
Label lbl_nme = (Label)clickedRow.FindControl("lbl_name");
String string_nme = lbl_nme.Text.ToString();
Session["Name"] = string_nme;
mpe.Show();
}
protected void panel_Load(object sender, EventArgs e)
{
Label1.Text = (string)(Session["Name"]);
}
So basically I have a GridView with name, address etc… When the user clicks on a link in a row, then the value for the name field of that row is saved as a session variable. Then a Modal PopUp is displayed. The Modal PopUp should then show the Name which was saved as a Session variable.
The code sort of works. What I’m experiencing is that when I click a row, the Label1.Text in the Modal PopUp is empty. So if I close the PopUp then click another link in another row, the PopUp then displays the Name of the row that was clicked previously.
In other words.. If row 1 has Name “Kevin” and row 2 has Name “Nathaniel”, and I click a link to open the Modal PopUp of row 1, I would expect the PopUp to display “Kevin”. But it doesn’t. The first time I click a link after rebuilding the application, nothing is displayed. But say I click row 2 after clicking row1, then the Modal PopUp displays the value of the row I clicked before, i.e. “Kevin” when I expect it to be “Nathaniel”.
I hope I didn’t confuse anyone. I’m a newbie and I’m just getting into this stuff, so I’d appreciate it if someone could help me out, preferably with examples of code etc.
Thank you. Much appreciated.
The "Load" event (panel_Load) occurs before the "Click" event (LinkButton1_Click) so it only sees the previous value.
The quick fix is to set the label in the "Click" event as well. Unless ViewState is enabled for the label (ick!) the label may have to be [re]set in the "Load" as well, depending upon when/how updates occur.
See ASP.NET Page Life Cycle Overview and ASP.NET Application and Page Life Cycle: Page Events.
Happy coding.

Another modal popup extender question

I am trying to get a modal popup to work, it needs to be triggered in the Code behind.
<asp:Button ID="btnModalPopUp" runat="server" Text="Button" Style="display: none" />
<asp:Panel ID="pnlModalPopup" runat="server" CssClass="modalPopup" Style="display: none"
Width="233px">
<div id="Div1" runat="server" cssclass="title">
Modal text here.
<asp:TextBox ID="txtEditComments" runat="server"></asp:TextBox>
</div>
</asp:Panel>
<cc1:ModalPopupExtender ID="modalMessage" runat="server" TargetControlID="btnModalPopUp"
PopupControlID="pnlModalPopup" BackgroundCssClass="modalBackground" DropShadow="true"/>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
modalMessage.Show();
}
Even though it hits the "modalMessage.Show();" code it doesn't show the modal panel.
Two solutions:
The first solution:
Remove Style="display:none" from the pnlModalPopup.
The first solution is will cause the popup to "flash" on the screen when the page first loads, and then quickly disappear.
The second solution:
protected void Page_Load(object sender, EventArgs e)
{
pnlModalPopup.Style["display"] = "block";
modalMessage.Show();
}
Recommendation:
I would recommend using the second solution, that way the modal popup doesn't flicker and then disappear.
Edit: I just tested your code:
I just tested your code in a simple page that contained only the code that you provided...It worked like expected.
Check the following:
Is your modal popup is defined in an UpdatePanel that is conditionally updated?
Check to make sure that the modal popup isn't defined in a Panel that has it's visibility set to false.
If that doesn't work, then check if the modal popup is actually in the source code of the rendered web page.
Listen to Chris's comment as it is needed:
display:none is cosmetically needed,
otherwise the popup will display when
the page is loading, then will quickly
disappear while the ModalPopupExtender
kicks in and hides it.
We had to make ours show like this:
pnlModalPopup.Visible = true;
modalMessage.Show();

Categories