Bootstrap Modal fires but not ASP Button Click event - c#

Basically just like the title says.
Layout:
Page w/ a DataGrid and a dynamic amount of buttons.
Intended functionality:
When one of the buttons is clicked, the modal will show, then the asp button click event will fire.
Functionality as of right now:
ASP Button is clicked, Modal shows, Event will not fire.
Anyone know if its possible to get both to fire?
Here is my modal:
div id="example" class="modal hide fade in" style="display: none;">
<div class="modal-header">
<a class="close" data-dismiss="modal">x</a>
<h3>Enter Comments Below:</h3>
</div>
<div class="modal-body">
<h4>Comments</h4>
</div>
<div class="modal-footer">
Call to action
Close
</div>
</div>
Here is the button:
<asp:Button ID="TestButton2" runat="server" Text="Comments"
OnClick="TestButton_Click"/>
Here is the buttons click event from the code Behind.
protected void TestButton_Click(object sender, EventArgs e)
{
// This does something
}
Here is the Jquery that shows the modal when the button is clicked:
<script type="text/javascript">
$('input[name$="TestButton2"]').on("click", function () {
$('#example').modal('toggle');
return false;
});
</script>
Anyone have any ideas? I've thought about a hidden field but don't know if that would really work. It may just not be possible with WebForms but thought I would ask.
Thanks in advance for your help.

call java script using ClientScriptManager on your button click
protected void TestButton_Click(object sender, EventArgs e)
{
// do something then call modal
ClientScript.RegisterStartupScript(GetType(), "Show", "<script> $('#example').modal('toggle');</script>");
}

Related

Asp.Net unable to fire event on button click

I'm new to ASP.Net. I am trying to trigger action based on button click but the even is not getting triggered.
This is the button definintion in login.aspx:
<button type="submit" value="submit" class="btn_3" onclick="createAccount_Clicked" runat="server">
Create Account
</button>
This is the intended button click event inside login.aspx.cs:
protected void createAccount_Clicked(object sender, EventArgs e)
{
//Here's where you do stuff.
string value = new_fullname.Value.ToString();
}
new_fullname is the Text I'm trying to read (defined in login.aspx):
<div class="col-md-12 form-group p_star" runat="server">
<input type="text" class="form-control" id="new_fullname" name="name" value=""
placeholder="Full Name" runat="server">
</div>
So, you have set the button type to submit which actually submits the server form which does not guarantee the triggering of the intended event. What you should do is, set the button type to button to get it working.
This is how you will get it working:
<button type="button" value="submit" class="btn_3" onclick="createAccount_Clicked" runat="server">
Create Account
</button>

Bootstrap Modal flashes/disappears instantly - ASP.NET gridview RowCommand

Good morning everybody. I seem to be stuck between a rock and a hard place and fear I may be missing something incredibly simple. I am currently developing a webforms application using an ASP.NET GridView control.
I have a bootstrap modal that is intended to display gridview row details through an ASP button on each row. I understand there are other posts on this topic but my issue seems to be a little different.
The button I use to toggle the bootstrap modal is in an asp control:
<asp:TemplateField HeaderText="Approval Information">
<ItemTemplate >
<asp:Button Text="Stats" runat="server" CommandName="ApprovalModal" type="button" class="btn btn-info" OnClientClick="return false" data-toggle="modal" data-target="#myModal"/>
</ItemTemplate>
</asp:TemplateField>
This button can currently toggle my modal just fine. The problem is, I want to subscribe to the RowCommand event in GridView so I can change the text of the ASP Literal control in the modal to display Row stats for each individual row when clicked:
protected void gvwApprovals_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ApprovalModal")
{
approvalModalText.Text = "Some Unique Row Stats";
approvalModalText.Visible = true;
}
}
The only way I can subscribe to this event and have it fire is if I take out OnClientClick="return false"
However, if I take that out of the ASP button, I subscribe to the event, but then the bootstrap modal now just flashes and disappears.
Here is my modal:
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="display: block;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Approval Information - Relevant Statistics</h4>
</div>
<div class="modal-body" >
<asp:Literal ID="approvalModalText" runat="server" Text='placeholder' Visible="false" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
What am I missing here? Available to clarify any further questions. Fairly new to ASP controls in general, so I'm not sure if there's something super simple I might just be totally unaware of.
EDIT:
Of course there is a post that happens to fire the RowCommand which reloads the page and the modal disappears. Thanks to Alex for pointing it out to me in the comments (I knew it was something dumb and simple). Must use RegisterStartupScript in Row Command handler to show the modal rather than just doing a straight toggle. jQuery here I come.

Modal Button Click Event

I have a modal that pops up when you click a button, inside that modal I have a save button. When that save button is pressed I would like to fire some C# code in a with an OnClick function....
Can anyone tell me why this isn't working for me?
ASP.NET
<div class="modal fade" id="deviceModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>Select</h4>
</div>
<div class="modal-body">
<asp:DropDownList ID="drpDownID" runat="server" CssClass="fields" EnableViewState="true">
<asp:ListItem Text="<Select>" Value="0" />
</asp:DropDownList>
</div>
<div class="modal-footer">
<asp:Button ID="btnSave" runat="server" Text="Save" data-dismiss="modal" CssClass="btn btn-primary" OnClick="btnSave_Click" />
<asp:Button ID="btnClose" runat="server" Text="Close" data-dismiss="modal" CssClass="btn btn-danger" OnClick="btnClose_Click" />
</div>
</div>
</div>
</div>
C#
protected void btnSave_Click(object sender, EventArgs e)
{
string test = "";
test = drpDownID.SelectedItem.ToString();
}
It won't even hit my break point.... And this is embedded in a form tag with a runat server
It works fine if I take the save button outside of the modal, but that completely defeats the purpose of the modal. So it has to be something with modal don't like click events very much...
Have you tried adding () to the onclick? like OnClick="btnSave_Click()".
EDIT: You can probably ignore that. It doesn't seem to matter for asp tags. Maybe this question can help you: ASP.NET button inside bootstrap modal not triggering click event
If your click on the save doesn't trigger a postback inside the dialog and it does outside the dialog: Can you use Firebug in Firefox to see whether the Save button is still part of the form?
I know JQuery dialog brings the container to the bottom of the body and therefor takes it out of a possible form, resulting in form posts not triggering from dialogues. I have a nice fix for this JQuery dialog issue, but that's probably not part of this topic.

bootstrap modal popup c# codebehind

i am using bootstrap in my c# asp.net project and i want to show to show the modal popup from code behind.
in my page header i have a javascript function as follows:
function LoginFail() {
$('#windowTitleDialog').modal('show');
}
and on the click of my button i am calling the javascript as follows
ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "LoginFail", "LoginFail();", true);
this does not show the modal popup. however, if i use something like alert('login failed'), it works fine.
can anybody please help with this?
By default Bootstrap javascript files are included just before the closing body tag
<script src="vendors/jquery-1.9.1.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="vendors/easypiechart/jquery.easy-pie-chart.js"></script>
<script src="assets/scripts.js"></script>
</body>
I took these javascript files into the head section right before the body tag and I wrote a small function to call the modal popup:
<script src="vendors/jquery-1.9.1.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="vendors/easypiechart/jquery.easy-pie-chart.js"></script>
<script src="assets/scripts.js"></script>
<script type="text/javascript">
function openModal() {
$('#myModal').modal('show');
}
</script>
</head>
<body>
then I could call the modal popup from code-behind with the following:
protected void lbEdit_Click(object sender, EventArgs e) {
ScriptManager.RegisterStartupScript(this,this.GetType(),"Pop", "openModal();", true);
}
This is how I solved:
The Script function
<script type="text/javascript">
function LoginFail() {
$('#windowTitleDialog').modal();
}
</script>
The Button declaration
<asp:Button ID="LaunchModal" runat="server" Text="Launch Modal" CssClass="btn" onclick="LaunchModal_Click"/>
Notice that the attribute data-togle="modal" is not set.
The Server-side code in the LaunchModal_Click event is:
ScriptManager.RegisterStartupScript(this, this.GetType(), "LaunchServerSide", "$(function() { LoginFail(); });", true);
I hope this help you.
I assume, you are using one of jQuery modal plugins. In that case, I suspect, plugin code isn't initialized at the time it is called (which would be immediately after it is rendered into page). You have to postpone call to the function after plugin code is executed. Implementation for your case might look like this:
ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "LoginFail", "$(function() { LoginFail(); });", true);
I have the same problem, tried to find online solution but i was unable to do so. I am able to call other javascript funcionts as you said from the code behind but when i add the modal js function the modal does not show up.
My guess is that the modal.('show') function is not being call because when i insepct in crhome the modal element the class is modal hide fade and not modal hide fade IN and other attributes stay the same.
A possible solution is to change those attributes from the code behind, i mean you can do what the js does from the code behind, possible not the best solution but i did not what else to do. By doing this i got the modal to show up with the background grey but could not make the effect to work.
I do this in the aspx page:
<asp:Panel ID="MessagePanel" runat="server" CssClass="hide" EnableViewState="false">
<div class="modal-header">
<asp:Button type="button" ID="btnClose" runat="server" data-dismiss="modal" Text="x" />
</div>
<div class="modal-body">
<h4>What's new in this version</h4>
... rest of bootstrap modal stuff....
</div>
</asp:Panel>
Then in code-behind to show the modal popup on any event such as page.load or a button click I just change the CssClass of the Panel:
MessagePanel.CssClass = "modal fade in"
No need for js or ScriptManager.

Strange scrollbar behavior with jQuery-validation

I wanted the scroll bar to scroll to the bottom when I clicked the Add Dependent linkbutton, and this works.
The problem is when the Add Dependent linkbutton is selected after the postback the text “Please fix the following problems:” shows for a second. But, there are no error messages below it. The validation (jQuery-validation plug-in) works normally if there are validation issues.
Thanks.
Here is the relevant asp.net code:
<script type="text/javascript" language="javascript">
function toBottom(id) {
document.getElementById(id).scrollTop = document.getElementById(id).scrollHeight
}
</script>
<div id="divscroll" class="scroll">
</div>
<div id="validationMessageContainer" class="error" >
<span id="info_dep_fix_problemls">Please fix the following problems:</span>
<ul></ul>
</div>
<asp:LinkButton ID="AddDependent" Text="Add Dependent" CssClass="btngeneral" OnClientClick="toBottom('divscroll')"
OnClick="AddDependent_Click" runat="server" />
C# Code behind:
protected void AddDependent_Click(object sender, EventArgs e)
{
//Other unrelated code -- the button has to be server side as we do database connections, etc.
getScrollPosition();
}
protected void getScrollPosition()
{
//Sets the scroll position of the divscroll to the bottom on postback
ClientScript.RegisterStartupScript(this.GetType(), "toBottom", "toBottom('divscroll')", true);
}
Here is the css for the scrolling div.
div.scroll
{
-ms-overflow-x:hidden;
overflow-x:hidden;
-ms-overflow-y:auto;
overflow-y:auto;
max-height: 400px;
max-width: 900px;
}
We figured it out, adding the style="display:none" to the validationMessageContainer div, fixed it.
Thanks to everyone who helped.
Try to add at the end of the JavaScript a return false; or maby that appear from code behind? It is unclear. You mean jquery validation plugin? If it is a form validatior, the look in to the plugin code.

Categories

Resources