ASP.Net C# WebForm: Putting A LinkButton In GridView - asp.net

I'm trying to put the following LinkButton into a GridView. Note the data-extend element of the LinkButton.
<asp:GridView ID="gvButtons" runat="server" AutoGenerateColumns="False" DataKeyNames="TrainingSwipeID"
CssClass="table table-bordered table-hover" ShowFooter="False">
<Columns>
<asp:TemplateField HeaderText="QuestionID" SortExpression="TrainingSwipeID">
<ItemTemplate>
<asp:LinkButton id="btnExtend" runat="server"
class="open-AddBookDialog btn btn-outline-secondary btn-med" href="#addBookDialog"
data-extend='{"TrainingSwipeID":<%# Eval("TrainingSwipeID") %>,"CurrentDate":"<%# Eval("TrainingEnd") %>"}'
data-toggle="modal" data-target="#myModal" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="TrainingSwipeID">
<ItemTemplate>
<asp:Label id="lblSwipeID" runat="server" Text='<%# Eval("TrainingSwipeID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="TrainingEnd">
<ItemTemplate>
<asp:Label id="lblExtendDate" runat="server" Text='<%# Eval("TrainingEnd") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The problem is the code in the LinkButton
data-extend='{"TrainingSwipeID":<%# Eval("TrainingSwipeID") %>,"CurrentDate":"<%# Eval("TrainingEnd") %>"}'
When the page gets rendered I get the following as an example for the LinkButton.
<tr>
<td>
<a id="gvButtons_btnExtend_0" class="open-AddBookDialog btn btn-outline-secondary btn-med" href="#addBookDialog" data-extend="{"TrainingSwipeID":<%# Eval("TrainingSwipeID") %>,"CurrentDate":"<%# Eval("TrainingEnd") %>"}" data-toggle="modal" data-target="#myModal" href="javascript:__doPostBack('gvButtons$ctl02$btnExtend','')"></a>
</td>
<td>
<span id="gvButtons_lblSwipeID_0">135</span>
</td>
<td>
<span id="gvButtons_lblExtendDate_0">2/10/2019 9:00:00 AM</span>
</td>
</tr>
How do I get the data-extend element within the LinkButton to render properly? It should render like this.
data-extend='{"TrainingSwipeID":135,"CurrentDate":"2/10/2019 9:00:00 AM"}'
ADDITIONAL INFO:
Here is the Javascript I use to open the modal dialog when the user clicks on the LinkButton:
<script>
$(function () {
$('#myModal').on('show.bs.modal', function (e) {
var ele = e.relatedTarget;
var sTrainingSwipeID = $(ele).data('extend').TrainingSwipeID;
var sCurrentDate = $(ele).data('extend').CurrentDate;
$("#TrainingSwipeID").val(sTrainingSwipeID);
$("#CurrentDate").val(sCurrentDate);
});
});
</script>
As well, here is the modal:
<!-- The Modal -->
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Extend Date</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<asp:HiddenField ID="TrainingSwipeID" runat="server"></asp:HiddenField>
<asp:TextBox ID="CurrentDate" runat="server" autocomplete="off"></asp:TextBox>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<asp:Button ID="btnExtendFromModal" runat="server" Text="Extend" class="btn btn-primary" OnClick="btnExtendDate_Click" UseSubmitBehavior="false" data-dismiss="modal" />
<button type="btnClose" class="btn btn-outline-secondary btn-med" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Thanks #AlexKudryashev, you were able to help me get to the final solution. It seems there is a "bug" with ASP.Net in returning single and double quotes in strings (in my case used for Javascript) from code behind with server side controls. So finally tried the following which worked.
Create a data table and get each requested row from the database.
Format a text string from each returned row from the database and put it into a row in the data table.
Use the data table as the data source for the GridView.
Use a regular hyperlink tag in the GridView, not using the runat="server".
For reference:
Formatted string from the database in code behind to be put into the data table in the DataExtendText column:
sDataExtendText = "{\"TrainingSwipeID\":" + iTrainingSwipeID + ",\"CurrentDate\":\"" + sNextReviewDateNoFormat + "\"}";
The regular hyperlink tag in the GridView getting it's data from the DataExtendText column in the data table:
<a id="btnExtend" class="btn btn-outline-success btn-med" data-extend='<%# Eval("DataExtendText") %>' data-toggle="modal" data-target="#myModal">Extend</a>

Related

button inside modal loads the page even if validations fire

I have a form inside which I have a button1 which triggers the modal.
Inside modal window I have textboxes(to take some credentials) with validations and another button (button2) (type=button) containing onClick definition which is working fine if I provide good data, but when I provide no data in one of the text boxes and click on the button2 the complete page reloads and I get to my main page where I click on button1 and I see the validation messages.
The validation should appear just as the user clicks button2
I have looked around and tried couple of thing but cant figure out why is it happening,
I am using page routing in page_load is that the reason?
here is the code inside the modal in my html.aspx:
<form=from1 runat=server>
<div class="modal fade" id="logger_modal" tabindex="-1" role="dialog" aria-labelledby="logger_model1" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body align-content-center">
<asp:TextBox ID="textbox1" runat="server" Class="form-control" placeholder="sometext" onkeydown = "return (event.keyCode!=13);"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="message1" ControlToValidate="textbox1" EnableClientScript="false" Display="Dynamic" ></asp:RequiredFieldValidator>
<asp:TextBox ID="textbox2" runat="server" Class="form-control" placeholder="sometext" onkeydown = "return (event.keyCode!=13);"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="message2" ControlToValidate="textbox2" EnableClientScript="false" Display="Dynamic"></asp:RequiredFieldValidator>
</div>
<div class="modal-footer">
<asp:Button ID="close" runat="server" Text="Cancel" type="button" class="btn btn-secondary" data-dismiss="modal"/>
<asp:Button ID="button2" runat="server" Text="button2_text" type="button" class="btn btn-primary" CausesValidation="true" OnClick="OnClick_method" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</div>
</div>
</div>
Code inside ONClick_method in html.aspx.cs
if (Page.IsValid)
{
some code which works if i provide right values in textboxes
}
else
{
Label1.Text = "Please provide the details.";
}
This happens because your page does a postback and the page does a complete reload which results in closing the modal which causes probably the lose of values.
To prevent the page from completely reloading you can use a UpdatePanel for partial update of your page.
Example:
<div class="modal-body align-content-center">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="textbox1" runat="server" Class="form-control" placeholder="sometext" onkeydown="return (event.keyCode!=13);"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="message1" ControlToValidate="textbox1" EnableClientScript="false" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:TextBox ID="textbox2" runat="server" Class="form-control" placeholder="sometext" onkeydown="return (event.keyCode!=13);"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="message2" ControlToValidate="textbox2" EnableClientScript="false" Display="Dynamic"></asp:RequiredFieldValidator>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="button2" />
</Triggers>
</asp:UpdatePanel>
</div>
You only surround the part that needs to be partially updated so the modal won't close and set the control for the async postback to button2.

When using multiple bootstrap modals on a single page (not displaying them at the same time) only the first modal works

I am having a strange problem and have spent the majority of the last 4 working hours trying to figure this out. I have read most of the similar questions on here but they are either not working due to a div mismatch or are wanting to open one modal from another.
I have a gridview containing data records with 3 link buttons in a template field. 2 of these buttons need to call up a modal where the user can either view more details or cancel the record.
The problem I am facing is that whichever modals code I put first in the aspx page, that modal displays correctly. Any modals after that only displays the black overlay.
I have read somewhere that this may be due to the z-index but I have not been able to get this working. Any guidance will be appreciated.
Gridview Markup:
<asp:GridView ID="gvEnquiries" runat="server" CssClass="table table-hover table-striped" GridLines="None" AutoGenerateColumns="False" DataKeyNames="EnquiryID">
<Columns>
<asp:BoundField HeaderText="EnquiryID" DataField="EnquiryID" Visible="false" />
<asp:BoundField HeaderText="Enquiry No" DataField="EnquiryNo" />
<asp:BoundField DataField="EType" />
<asp:BoundField HeaderText="Company" DataField="Company" />
<asp:BoundField HeaderText="Description" DataField="Description" />
<asp:BoundField HeaderText="Assigned To" DataField="AssignedTo" />
<asp:BoundField HeaderText="Address By" DataField="AddressBy" DataFormatString="{0:dd MMM yyyy}" />
<asp:BoundField HeaderText="Enquiry Date" DataField="EnquiryDate" DataFormatString="{0:dd MMM yyyy}" />
<asp:TemplateField>
<ItemTemplate>
<div class="btn-group pull-right">
<asp:LinkButton CssClass="btn btn-info" ID="btnView" runat="server" data-toggle="tooltip" data-placement="bottom" ToolTip="View Details" OnClick="btnView_Click"><span class="glyphicon glyphicon-eye-open"></span></asp:LinkButton>
<asp:LinkButton CssClass="btn btn-info" ID="LinkButton1" runat="server" data-toggle="tooltip" data-placement="bottom" ToolTip="Create Quote"><span class="glyphicon glyphicon-credit-card"></span></asp:LinkButton>
<asp:LinkButton CssClass="btn btn-info" ID="btnCloseEnq" runat="server" data-toggle="tooltip" data-placement="bottom" ToolTip="Close Enquiry" OnClick="btnCloseEnq_Click"><span class="glyphicon glyphicon-ban-circle"></span></asp:LinkButton>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The modals are being called from code side using the following line of code, obviously changing the ID for the seperate calls:
ScriptManager.RegisterStartupScript(this, this.GetType(), "none", "<script>$('#CloseEnq').modal('show');</script>", false);
The actual markup for the 2 modals are:
<div class="modal fade" id="ViewEnqDetail" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-mid modal-dialog">
<div class="modal-content">
<asp:UpdatePanel ID="updViewEnqModal" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">
<asp:Label runat="server" ID="lblViewEnqTitle"></asp:Label></h4>
</div>
<div class="modal-body">
<div class="container-fluid">
****Body Content In Here*******
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="modal-footer">
<button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
</div>
and
<div class="modal fade" id="CloseEnq" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-mid modal-dialog">
<div class="modal-content">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">
<asp:Label runat="server" ID="Label1"></asp:Label></h4>
</div>
<div class="modal-body">
<div class="container-fluid">
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="modal-footer">
<button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
</div>
After another 7 hours I have finally found the culprit.
The order in which the tags were closed were incorrect. The body div was only being closed after the update panel and its content div was.

Load and Save Data in Bootstrap popup modal in ASP.NET

I'm using master page for my web application there is a content page we can call it Banks data has a GridView displays all banks and has an Edit,Add,Delete buttons
I wand display a bootstrap modal with input data to edit or add new bank and to validate using asp RequiredFieldValidator
this is the ASPX Code
<Columns>
<asp:BoundField DataField="bankCode" HeaderText="Bank Code" />
<asp:BoundField DataField="bankName" HeaderText="Bank Name" />
<asp:TemplateField >
<ItemTemplate >
<asp:LinkButton ID="lb_Edit" runat="server" CssClass="btn btn-sm btn-default" data-toggle="modal" data-target="#AddBankModal" CommandName="BankEdit" CommandArgument='<%#Eval("bankCode") %>' OnClick="lb_Edit_Click"><strong>Edit</strong></asp:LinkButton>
<asp:LinkButton ID="LB_Delete" runat="server" CssClass="btn btn-sm btn-danger" CommandName="BankDelete" CommandArgument='<%#Eval("bankCode") %>'><strong>Delete</strong></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle Height="15px" HorizontalAlign="Right" VerticalAlign="Middle" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [bankCode], [bankNameAr], [bankNameEng] FROM [Bank]"></asp:SqlDataSource>
<!-- Modal -->
<div id="AddBankModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content" >
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" style="text-align:right">Bank Data </h4>
</div>
<div class="modal-body">
<asp:Label ID="lblBankCode" runat="server" Text="Bank Code" CssClass="label" style="text-align:right;font-weight:bold"></asp:Label>
<asp:TextBox ID="txtBankCode" runat="server" CssClass="form-control"></asp:TextBox>
<asp:Label ID="lblBankNameAr" runat="server" Text="Bank Name" CssClass="label" style="text-align:right;font-weight:bold"></asp:Label>
<asp:TextBox ID="txtBankNameAr" runat="server" CssClass="form-control"></asp:TextBox>
<div class="row" >
<div class="col-lg-12">
<asp:Button ID="btnUpdateBank" runat="server" Text="Save Data" CssClass="btn btn-outline btn-primary" style="width:100px" OnClick="btnUpdateBank_Click" />
<button type="button" class="btn btn-outline btn-primary" data-dismiss="modal" style="width:100px">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
Does any one have any Ideas to do this using ASP code behind without using Jquery
May be you need some thing like this.Remember two things
1.Dont forget to add ClientIDMode="Static" to asp.net control
2.Postback is prevented by e.preventDefault() to add jQuery functionality in click event
<asp:GridView ID="GridView1" runat="server" ClientIDMode="Static">
<Columns>
<asp:TemplateField >
<ItemTemplate >
<asp:LinkButton ID="lb_Edit" runat="server" CssClass="btn btn-sm btn-default" data-toggle="modal" data-target="#AddBankModal" ClientIDMode="Static"><strong>Edit</strong>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="AddBankModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
×</button>
<h4 class="modal-title" style="text-align: right">
Bank Data
</h4>
</div>
<div class="modal-body">
<asp:Label ID="lblBankCode" runat="server" Text="Bank Code" CssClass="label" Style="text-align: right;
font-weight: bold"></asp:Label>
<asp:TextBox ID="txtBankCode" runat="server" CssClass="form-control"></asp:TextBox>
<asp:Label ID="lblBankNameAr" runat="server" Text="Bank Name" CssClass="label" Style="text-align: right;
font-weight: bold"></asp:Label>
<asp:TextBox ID="txtBankNameAr" runat="server" CssClass="form-control"></asp:TextBox>
<div class="row">
<div class="col-lg-12">
<asp:Button ID="btnUpdateBank" runat="server" Text="Save Data" ClientIDMode="Static" CssClass="btn btn-outline btn-primary"
Style="width: 100px" />
<button type="button" class="btn btn-outline btn-primary" data-dismiss="modal" style="width: 100px">
Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(function () {
$('.btn').on('click', function (e) {
e.preventDefault();
var buttonid = $(this).attr('id');
console.log(buttonid);
if (buttonid == "lb_Edit") {
$('#AddBankModal').modal({
show: true,
backdrop: 'static'
});
}
if (buttonid == "btnUpdateBank") {
//
alert('Do some ajax to send data to Backend');
}
//currentRow.remove();
});
})
</script>

Cannot hide Panel in Grdivew - Object reference not set to an instance of an object

I am trying to hide a Panel in Gridview but I get
Object reference not set to an instance of an object.
Here is the code I am using:
Protected Sub gvTally_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvTally.RowDataBound
If e.Row.Cells(2).Text = "Incomplete" Then
Dim Panelhide As Panel = CType(e.Row.FindControl("Panel1"), Panel)
Panelhide.Visible = False
Else
Dim Panelhide As Panel = CType(e.Row.FindControl("Panel1"), Panel)
Panelhide.Visible = True
End If
End Sub
And here is my Gridview:
<asp:GridView ID="gvTally" runat="server" EnableModelValidation="True"
AllowSorting="True" OnRowDataBound="gvTally_RowDataBound" class="table table-condensed table-striped table-bordered table-hover no-margin" AutoGenerateColumns="False" Font-Size="Small">
<Columns>
<asp:BoundField DataField="Company" HeaderText="Company">
<ItemStyle Width="180px" />
</asp:BoundField>
<asp:BoundField DataField="Submitted" HeaderText="Submitted" />
<asp:BoundField DataField="Variance" HeaderText="Variance" />
<asp:BoundField DataField="Accepted" HeaderText="Accepted" />
<asp:TemplateField HeaderText="Action" ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" CssClass="btn btn-warning btn-small" Text="Unaccept" CommandName="Unaccept" Visible="False" />
<asp:Button ID="Button2" runat="server" CssClass="btn btn-success btn-small hidden-phone" Text="Accept" CommandName="Accept" />
<asp:Panel ID="Panel1" runat="server">
<a href="#accSettings3" role="button" class="btn btn-success btn-small hidden-phone" data-toggle="modal" data-original-title="">Accept
</a>
<div id="accSettings3" class="modal hide fade" tabindex="-3" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 id="myModalLabel3">Accept Validation
</h4>
</div>
<div class="modal-body">
<div class="row-fluid">
Are you sure you want to accept the tally with the variance off?
</div>
</div>
<div class="modal-footer">
<asp:Button ID="btnacceptchanges" class="btn btn-primary" runat="server" Text="Yes" />
<button class="btn" data-dismiss="modal" aria-hidden="true">
No
</button>
</div>
</div>
</asp:Panel>
<asp:Button ID="Button1" runat="server" CssClass="btn btn-info btn-small" Text="View" CommandName="View" />
</td>
</tr>
</ItemTemplate>
<ItemStyle Width="160px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
I am not sure why I cannot hide this Panel that contains other HTML and another Button Control even when I use FindControl.
I have other FindControls that work perfectly fine, but this one gives me the error that it cannot find the Panel1.
I am using the panel just to hide the code inside it.
This probably has nothing to do with the panel.
Make sure you have a data row and not a header or something.
If e.Row.RowType = DataControlRowType.DataRow Then
...
End If

Populate bootstrap modal from gridview edit link button

I have a gridview containing membership providers roles.
With Link Button For editing and Link button for deleting.
I want to populate a modal with a text box inside it to save the role name as shown.
I succeeded in populating the modal and binding the grid and all of that, but, how can I bind the txtRoleName in the modal with the role name of the row? Preferably without any postback if possible, or with postback if not.
Here is the code for the grid:
<asp:GridView ID="grdRoles"
CssClass="table table-bordered responsive"
runat="server"
GridLines="None"
CellSpacing="-1"
AutoGenerateColumns="False"
OnPageIndexChanging="grdRoles_PageIndexChanging"
OnRowDataBound="grdRoles_RowDataBound"
ShowFooter="True" ShowHeaderWhenEmpty="True" EmptyDataText="Empty !">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<asp:Label ID="lblRank" runat="server" Text='<%# Container.DataItem %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Roles">
<ItemTemplate>
<asp:Label ID="lblRoleName" runat="server" Text='<%# Container.DataItem %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" data-toggle="modal" href="#EditModal" runat="server" CssClass="btn icon-edit" />
<asp:LinkButton ID="btnRemove" runat="server" CssClass="btn btn-danger remove" Text="<i class=icon-remove></i>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And this is the code for the modal:
<div id="EditModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="Edit"
aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="helpModalLabel"><i class="icon-external-link"></i> Edit Role</h3>
</div>
<div class="modal-body">
<div class="control-group">
<div class="controls">
<div class="input-prepend">
<span class="add-on">Role Name</span>
<asp:TextBox ID="txtRoleName" Text='<%# Bind("RoleId") %>' runat="server"></asp:TextBox>
</div>
</div>
<div class="form-actions">
<asp:Button ID="btnSave" type="submit" class="btn btn-primary" runat="server" Text="Save" />
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
I am using data property to send data to the modal:
<a id="edit" href="#dlgName" data-toggle="modal" role="button"
data-id="<%# DataBinder.Eval (Container.DataItem, "Id") %>"
data-IsPrimary ="<%# DataBinder.Eval(Container.DataItem, "IsPrimary") %>" >Edit</a>
And, retrieve the data using jQuery:
var Id = $('#edit').data('Id');
var isprimary = $('#edit').data('IsPrimary');

Resources