Textbox value in gridview in updatepanel - updatepanel

trying to get value from textbox called tbAantalgv in a gridview within an updatepanel after button click. standard value after databind for tbAantalgv = 0, but when i change the textbox value to 1, the textbox value will NOT change.
For Each gvr As GridViewRow In gvOnderdelenLos.Rows
If gvr.RowType = DataControlRowType.DataRow Then
Dim lblCode As Label = gvr.FindControl("lblCode")
Dim lblArtikel As Label = gvr.FindControl("lblArtikel")
Dim tbAantalgv As TextBox = gvr.FindControl("tbAantalgv")
End If
Next
<asp:UpdatePanel ID="pnlOnderdelenLos" runat="server" style="width:95%; border:solid 3px white; display:none;" UpdateMode="Conditional" >
<ContentTemplate>
<asp:ImageButton ID="imgOnderdelenLosToevoegen" runat="server" OnClientClick="imgOnderdelenLosToevoegen()" ImageUrl="~/Afbeeldingen/Icons/table_row_insert.png"
style="vertical-align:text-top; float:right; margin-right:2%" />
<asp:SqlDataSource ID="dsOnderdelenLos" runat="server" ConnectionString="<%$ ConnectionStrings:AftersalesConnectionString %>"
SelectCommand="EXEC spAppPnlOnderdelenLos">
</asp:SqlDataSource>
<asp:GridView ID="gvOnderdelenLos" runat="server" DataSourceID="dsOnderdelenLos" AllowSorting="False" PagerSettings-PageButtonCount="20" ShowHeader="false"
AllowEditing="True" AutoGenerateColumns="False" CellPadding="3" GridLines="Both" AllowPaging="False" style="width:100%; margin-left:2px;">
<HeaderStyle CssClass="Gridview" />
<Columns>
<asp:TemplateField HeaderText="Artikelnummer" SortExpression="Code" ItemStyle-Width="25%" >
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Bind("Code")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Artikel" SortExpression="Omschrijving" ItemStyle-Width="25%">
<ItemTemplate>
<asp:Label id="lblArtikel" runat="server" Text='<%# Bind("Omschrijving")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Aantal" SortExpression="Aantal" ItemStyle-Width="25%">
<ItemTemplate>
<asp:TextBox id="tbAantalgv" runat="server" Width="95%" text='<%# Bind("Aantal")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kostenplaats" SortExpression="Kostenplaats" ItemStyle-Width="25%">
<ItemTemplate>
<asp:DropDownList ID="dlKostenplaats" runat="server" Width="100%">
<asp:ListItem Text="Werkplaats"></asp:ListItem>
<asp:ListItem Text="Verkoop"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnOnderdelenLosToevoegen" runat="server" OnClick="btnOnderdelenLosToevoegen_click" style="display:none;"/>
I do not understand what the issue can be.
Thanks in advance.
Frank

Related

why is there a <td> </td> in my gridview ASP.NET

I have a gridview which is generated by asp.net. I have a id row which I have hidden using css. However this causes a visual problem here is the problem in question.
I know that the problem is caused due to the last td in the html that looks like this <td> </td>.
Has you can see on this image.
when i remove that line of code it works and my gridview looks like this.
why does asp generate this code <td> </td>. how can i remove it any help would be very appreciated thank you.
Here is the code for my gridview
<asp:GridView ID="gvLocation" runat="server" AutoGenerateColumns="false" ShowFooter="true"
ShowHeaderWhenEmpty="true" AllowPaging="True" OnPageIndexChanging="gvLocation_PageIndexChanging"
OnRowEditing="gvLocation_RowEditing" OnRowCancelingEdit="gvLocation_RowCancelingEdit"
AllowSorting="true" onsorting="gvLocation_Sorting" ridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" CellPadding="3">
<%-- Theme Properties --%>
<Columns>
<asp:TemplateField HeaderText="Armario" SortExpression="armario">
<ItemTemplate>
<asp:Label CssClass="gridTextbox" ID="lblCloset" Text='<%# Eval("armario")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtCloset" Text='<%# Eval("armario")%>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtClosetFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cajon" SortExpression="cajon">
<ItemTemplate>
<asp:Label CssClass="gridTextbox" ID="lblDrawer" Text='<%# Eval("cajon")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtDrawer" Text='<%# Eval("cajon")%>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtDrawerFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px" OnClick="deleteItem"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px" OnClick="updateItem"/>
<asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="~/Images/addnew.png" ID="addNewLocation" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px" OnClick="addNew"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ubicacion id" SortExpression="ubicacion_componente_id" ItemStyle-CssClass="hideGridColumn" HeaderStyle-CssClass="hideGridColumn">
<ItemTemplate>
<asp:Label CssClass="gridTextbox" ID="lblLocationID" Text='<%# Eval("ubicacion_componente_id")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Add here is the code for the css that hides the first id column
.hideGridColumn
{
display: none;
}
There are 2 solution for you to remove redundant <td> </td>
Javascript version
var table = document.getElementById("mytable");
for (var i = 0; i < 3; i++) {
for (var k = 0; k < 5; k++) {
var str = table.rows[i].cells[k].innerHTML;
str = str.replace(/( )*/g, '');
if (str.length) alert(str);
}
};
How to remove with javascript this case?
Jquery version
$(function() {
$("table tr").each(function() {
var tds = $(this).find("td"); //find all td
var descriptionTD = tds.eq(1); //get the td for the description
//get the remaining tds that only contain " "
var emptytds = tds.not(descriptionTD).filter(function() {
return $(this).html() === " ";
});
//if all the remaing tds are empty
if (emptytds.length === tds.length - 1) {
emptytds.remove();
descriptionTD.prop("colspan", tds.length).prop("width", "100%");
}
});
});
remove <td> that has and make sub header
<td> </td>
Its the recommended pattern, cause it works on every browser. Try to run your table without &nbsp in Edge Browser, you will see that breaks the table layout.
<td>/td>
Without '&nbsp'
<td> </td>
With '&nbsp'

How do I hide/show a DIV inside a grid ItemTemplate?

I want to set individual DIVs inside a grid visible/hidden programmatically like so, but it isn't working. How would you fix this code?
foreach (DataGridItem dgItem in dgW.Items)
{
HtmlGenericControl dvGoodRow =
(HtmlGenericControl)dgItem.FindControl("dvGoodRow");
HtmlGenericControl dvBadRow =
(HtmlGenericControl)dgItem.FindControl("dvBadRow");
dvGoodRow.Visible = true;
dvBadRow.Visible = false;
}
Debug sessions show that the Visible attribute I set above does take effect at least in my watch window, but visually on the browser I see no change. I have a grid like the one below:
<asp:datagrid id="dgW" AutoGenerateColumns="False" ShowHeader="False" ShowFooter="False" runat="server" DataKeyField="SID" CellPadding="0" GridLines="None" AllowSorting="True" OnItemDataBound="dgW_ItemDataBound">
<HeaderStyle />
<FooterStyle>
</FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="SID" SortExpression="SID">
<ItemTemplate>
<asp:Label ID="lblSID" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Extension" SortExpression="ExtensionStopDate">
<ItemTemplate>
<asp:Label id="lblExtensionStopDate" Runat="server" text='<%# String.Format("{0:MM/dd/yyyy}", DataBinder.Eval(Container.DataItem, "ExtensionStopDate"))%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<div id="dvGoodRow" runat="server" hidden>
<div>
<asp:Label runat="server" ID="lblGoodRow">
<i id="icnGoodRow" runat="server"></i>
</asp:Label>
</div>
</div>
<div id="dvBadRow" runat="server" hidden>
<div>
<asp:Label runat="server" ID="lblBadRow">
<i id="icnBadRow" runat="server"></i>
</asp:Label>
</div>
</div>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Remove that 'hidden' property and use visible
<div id="dvGoodRow" runat="server" visible="false">
<div id="dvBadRow" runat="server" visible="false">
Here's what I ended up doing to fix the code: I switched to showing/hiding ASP.NET Panels instead of DIVs!
<asp:TemplateColumn>
<ItemTemplate>
<asp:Panel runat="server" ID="pnlGoodRow" Visible="False">
<div id="dvGoodRow">
<asp:Label runat="server" ID="lblGoodRow">
<i id="icnGoodRow" runat="server"></i>
</asp:Label>
</div>
</asp:Panel>
<asp:Panel runat="server" ID="pnlBadRow" Visible="False">
<div id="dvBadRow">
<asp:Label runat="server" ID="lblBadRow">
<i id="icnBadRow" runat="server"></i>
</asp:Label>
</div>
</asp:Panel>
</ItemTemplate>
</asp:TemplateColumn>
So in the code, I now hide/show the Panel elements instead of DIVs:
foreach (DataGridItem dgItem in dgW.Items)
{
System.Web.UI.WebControls.Panel pnlGoodRow =
(System.Web.UI.WebControls.Panel)dgItem.FindControl("pnlGoodRow");
System.Web.UI.WebControls.Panel pnlBadRow =
(System.Web.UI.WebControls.Panel)dgItem.FindControl("pnlBadRow");
pnlGoodRow.Visible = true;
pnlBadRow.Visible = false;
}
This works!

How to add new html elements inside div tag, on basis of no.of items in list?

issue.aspx
<div id="model_input_stock" runat="server" class="form-group well">Dynamically need to insert new textbox and lable with in new div</div>
Issue.aspx.cs
protected void Button_Grid_DT_Assign_Click(object sender, EventArgs e)
{
//use assigned in selecting issued quantity view
foreach(var item in StockCode_List)
{
model_input_stock.InnerHtml.???
}
}
Is their any best alternative way, i'm new in ASP.NET??
You can add an ASP.net panel inside of your div for first and then inside of your foreach loop initialize a new textbox and add it to your panel:
Textbox txt = new Textbox();
txt.id = "txt1";
Txt.Text = "My Text";
Panel.Controls.Add(txt);
Use the following code to do so:
Default.aspx:
<div>
<asp:TextBox ID="txtRows" runat="server"></asp:TextBox>
<asp:Button SkinID="b" ID="btnAddRow" runat="server" Text="Click To Proceed" OnClick="btnAddRow_Click" Width="220px" />
<asp:GridView SkinID="grv" ID="GridViewProducts" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="SL No.">
<ItemTemplate>
<%#Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<asp:DropDownList ID="ddlCategoryID" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:DropDownList ID="ddlItemID" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Type">
<ItemTemplate>
<asp:DropDownList ID="ddlUnitTypeID" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDes" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div style="padding: 10px 0px;">
<asp:Panel ID="Panel1" runat="server" Visible="false">
<asp:Button ID="btnSave" runat="server" Text="Submit" OnClick="btnSave_Click" />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</asp:Panel>
</div>
In the code-behind, Default.aspx.cs:
private void AddRowsToGrid()
{
List<int> noofRows = new List<int>();
int noOfInputs = Convert.ToInt32(txtRows.Text);
for (int i = 0; i < noOfInputs; i++)
{
noofRows.Add(i);
}
GridViewProducts.DataSource = noofRows;
GridViewProducts.DataBind();
if (GridViewProducts.Rows.Count > 0)
{
Panel1.Visible = true;
}
else
{
Panel1.Visible = false;
}
}
protected void btnAddRow_Click(object sender, EventArgs e)
{
AddRowsToGrid();
}
Finally you'll have the following output giving input 10:

UpdatePanel does not update data, some of the time. Sometimes there are long delays

I have an UpdatePanel that has another Panel inside to mimic a simple pop-up window.
Inside the Panel I have a HTML Table, with several rows and a few columns, and a GridView in one row and a DetailsView further down.
There is also an SUBMIT and a CANCEL button on the HTML table side.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="ButtonSubmit" />
<asp:PostBackTrigger ControlID="ButtonCancel" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnlRoomDetails" runat="server" CssClass="pnlDetail" Visible="false" Width="500px">
<table id="tblDetails" runat="server" style="width: 100%">
<tr style="height:26px;">
<td colspan="6" style="font-size: 12pt; color: yellow; font-family: Arial; background-color: blue;
text-align: center" valign="middle">
<strong>Room Allocation</strong></td>
</tr>
<tr>
<td colspan="1" style="text-align: left; vertical-align: middle; width: 128px; height: 24px;" valign="top">
<strong>
Room ID</strong></td>
<td colspan="3" style="width: 357px; text-align: left; vertical-align: middle; height: 24px;">
<asp:Label ID="lblRoomID" runat="server" Text="Label" Font-Bold="true"></asp:Label></td>
<td colspan="1" style="vertical-align: middle; width: 106px; height: 24px; text-align: left">
<strong>
Available?</strong></td>
<td colspan="1" style="vertical-align: middle; width: 120px; height: 24px; text-align: left">
<asp:CheckBox ID="cbStatus" runat="server" Width="98px" Enabled="False" /></td>
</tr>
<tr>
<td colspan="1" style="text-align: left; vertical-align: middle; width: 128px; height: 24px;" valign="top">
<strong>
Name</strong></td>
<td colspan="3" style="width: 357px; text-align: left; vertical-align: middle; height: 24px;">
<asp:Label ID="lblRoomName" runat="server" Text="Label"></asp:Label></td>
<td colspan="1" style="vertical-align: middle; width: 106px; height: 24px; text-align: left">
<strong>
Configuration</strong></td>
<td colspan="1" style="vertical-align: middle; width: 120px; height: 24px; text-align: left">
<asp:Label ID="lblRoomConfigName" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td colspan="1" style="vertical-align: middle; width: 128px; height: 24px; text-align: left"
valign="top">
<strong>Max Beds</strong></td>
<td colspan="3" style="vertical-align: middle; width: 357px; height: 24px; text-align: left">
<asp:Label ID="lblBeds" runat="server" Text="Label"></asp:Label></td>
<td colspan="1" style="vertical-align: middle; width: 106px; height: 24px; text-align: left">
</td>
<td colspan="1" style="vertical-align: middle; width: 120px; height: 24px; text-align: left">
</td>
</tr>
<tr>
<td colspan="1" style="vertical-align: middle; width: 128px; height: 24px; text-align: left"
valign="top">
<strong>Room Comments</strong></td>
<td colspan="5" style="vertical-align: middle; height: 24px; text-align: left">
<asp:TextBox ID="tbComments" runat="server" Enabled="False" Height="47px" MaxLength="8"
ReadOnly="True" Style="text-align: left" TextMode="MultiLine" Width="98%"></asp:TextBox></td>
</tr>
<tr>
<td colspan="6">
<asp:Label ID="lblMsg" runat="server" Visible="False" Width="100%" BackColor="Red" Font-Bold="True" Font-Names="Arial" ForeColor="Yellow" style="text-align:center"></asp:Label>
<asp:GridView ID="gvAllocations" runat="server" AllowSorting="True" CellPadding="4"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" Font-Names="Arial Narrow" Font-Size="10pt"
DataSourceID="sdsAllocations" DataKeyNames="BoardingCheckinID"
OnRowDataBound="gvAllocations_RowDataBound"
OnDataBound="gvAllocations_DataBound">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Tenant" SortExpression="TenantName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("TenantName") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="TenantID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("TenantID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Bed" SortExpression="BedName">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("BedName") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Checked IN" SortExpression="DateCheckedIn">
<ItemTemplate>
<asp:Label ID="lblDateCheckIn" runat="server" Text='<%# Eval("DateCheckedIn","{0:g}") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField Visible="false" ShowHeader="false">
<ItemTemplate>
<asp:Label ID="lblCheckedInBy" runat="server" Text='<%# Eval("CheckedInBy") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expected Check Out" SortExpression="ExpectedCheckOut">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("ExpectedCheckOut","{0:g}") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes" SortExpression="Notes">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("Notes") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkDEL" runat="server" CausesValidation="False" CommandName="Delete" Text="DEL" OnClientClick="return confirm('Are you sure?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="sdsAllocations" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>" CancelSelectOnNullParameter="False"
SelectCommand="SELECT bc.BoardingCheckinID, bc.BoardingRoomID, ISNULL(bc.TenantID, bc.StudentID) AS TenantID, v1.StudentPreferredName + ' ' + v1.StudentFamilyName AS TenantName, bc.BedID, bb.BedName, bc.BoardingTypeID, brt.TypeName, bc.DateCheckedIn, bc.CheckedINby, bc.ExpectedCheckOut, bc.Notes FROM tblBoardingCheckins AS bc INNER JOIN vwBoardingTenants AS v1 ON v1.StudentID = bc.StudentID AND v1.Year = YEAR(GETDATE()) INNER JOIN tblBoardingBeds AS bb ON bb.BedID
= bc.BedID INNER JOIN tblBoardingRooms AS br ON br.BoardingRoomID = bc.BoardingRoomID INNER JOIN tblBoardingRoomTypes AS brt ON brt.BoardingTypeID = bc.BoardingTypeID WHERE (bc.IsActive = 1) AND (bc.DateCheckedIn <= GETDATE()) AND (bc.ExpectedCheckOut >= GETDATE()) AND (bc.BoardingRoomID = #RoomID) ORDER BY bc.DateCheckedIn DESC"
DeleteCommand="UPDATE tblBoardingCheckins SET IsActive = 0 WHERE BoardingCheckinID = #BoardingCheckinID">
<SelectParameters>
<asp:ControlParameter ControlID="lblSelectedRoomID" Name="RoomID" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="BoardingCheckinID" />
</DeleteParameters>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td colspan="6">
<asp:DetailsView ID="dvRoomCheckIn" runat="server" AutoGenerateRows="False" DefaultMode="Insert" CellPadding="3"
Width="100%" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" OnDataBound="dvRoomCheckIn_DataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<Fields>
<asp:BoundField DataField="BoardingBookingID" ReadOnly="True" Visible="False" />
<asp:TemplateField HeaderText="Pick Student">
<ItemTemplate>
<asp:DropDownList ID="ddlTenant" runat="server" DataSourceID="sdsTenants" DataTextField="StudentName" DataValueField="StudentID" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="Please select.." Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sdsTenants" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
SelectCommand="WITH cte AS (SELECT CASE WHEN bc.StudentID IS NULL THEN v1.StudentID ELSE NULL END AS StudentID, CASE WHEN bc.StudentID IS NULL THEN (v1.StudentPreferredName + ' ' + v1.StudentFamilyName) ELSE NULL END AS StudentName FROM vwBoardingTenants AS v1 LEFT OUTER JOIN tblBoardingCheckins AS bc ON v1.StudentID = bc.StudentID AND bc.DateCheckedIn <= GETDATE() AND bc.ExpectedCheckOut >= GETDATE() AND bc.IsActive = 1 WHERE (v1.Boarding = 'Y') AND (v1.Year = YEAR(GETDATE()))) SELECT StudentID, StudentName FROM cte AS cte_1 WHERE (StudentID IS NOT NULL) ORDER BY StudentName">
</asp:SqlDataSource>
<asp:RequiredFieldValidator ID="rfvTenant" runat="server" ControlToValidate="ddlTenant" ErrorMessage="Required" InitialValue="0" ValidationGroup="vg1"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Choose Bed">
<ItemTemplate>
<asp:DropDownList ID="ddlBeds" runat="server" DataSourceID="sdsBeds" DataTextField="BedName" DataValueField="BedID" OnDataBound="ddlBeds_DataBound">
<asp:ListItem Selected="True" Text="Select.." Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sdsBeds" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
SelectCommand="SELECT BedID, BedName FROM tblBoardingBeds WHERE BedID <= #BedMax ORDER BY BedName">
<SelectParameters>
<asp:ControlParameter ControlID="lblBeds" Name="BedMax" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Check IN">
<ItemTemplate>
<table width="100%">
<tr>
<td>
<asp:Label ID="lblCheckedIN" runat="server" Text='<%# Eval("DateCheckedIn","{0:g}") %>' Width="70%"></asp:Label>
</td>
<td>
<strong style="text-align: center">BY</strong>
</td>
<td>
<asp:Label ID="lblCheckedINby" runat="server" Text='<%# Eval("CheckedINby") %>' Enabled="false"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Check OUT by">
<ItemTemplate>
<table width="100%">
<tr>
<td>
<asp:TextBox ID="txtCheckOUT" runat="server" Text='<%# Bind("ExpectedCheckOut") %>' Width="80"></asp:TextBox>
<asp:ImageButton runat="Server" ID="calImg" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" CausesValidation="False" />
<ajaxToolkit:CalendarExtender PopupButtonID="calImg" Enabled="true" TargetControlID="txtCheckOUT" ID="ce1" runat="server" Format="d/MM/yyyy" PopupPosition="TopRight"></ajaxToolkit:CalendarExtender>
<asp:RequiredFieldValidator SetFocusOnError="true" ValidationGroup="vg1" ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtCheckOUT" ErrorMessage="Date is required">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtCheckOUT" Operator="DataTypeCheck" Type="Date" Display="Dynamic" ErrorMessage="Invalid Date (d/M/yyyy)" SetFocusOnError="True"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtCheckOUT" Operator="GreaterThan" Type="Date" Display="Dynamic" ValueToCompare='<%# DateTime.Today.ToShortDateString() %>' ErrorMessage="Check out date must be greater than Checked IN date!" SetFocusOnError="True"></asp:CompareValidator>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments" SortExpression="Notes">
<ItemTemplate>
<asp:TextBox ID="txtNotes" runat="server" Text='<%# Bind("Notes") %>' Width="98%" Rows="5" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<RowStyle ForeColor="#000066" />
</asp:DetailsView>
</td>
</tr>
<tr>
<td colspan="6" style="text-align: center">
<asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="Submit_Click" CausesValidation="true" ValidationGroup="vg1" />
<%--
<input id="ButtonSubmit" runat="server" value="Submit" type="button" validationgroup="vg1" onserverclick="Submit_Click" causesvalidation="true" />
<input id="ButtonSubmit" runat="server" type="button" value="Submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ButtonSubmit', '', true, 'vg1', '', false, false))"/>
--%>
<asp:Button ID="ButtonCancel" runat="server" Text="Cancel" OnClick="Cancel_Click" CausesValidation="false"/>
</td>
</tr>
</table>
</asp:Panel>
</ContentTemplate> </asp:UpdatePanel>
The Submit button fires this code ...
protected void Submit_Click(object sender, EventArgs e)
{
//find button used to get here
Button btnRoomClicked = (Button)pnlRoomDetails.FindControl("Room" + lblSelectedRoomID.Text);
if (btnRoomClicked != null)
{
//reset colours
btnRoomClicked.Font.Bold = false;
}
//Check In!
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ATCNTV1ConnectionString"].ConnectionString))
{
cnn.Open();
try
{
//prepare command
using (SqlCommand cmd = new SqlCommand("spBoardingCheckIN", cnn))
{
cmd.CommandType = CommandType.StoredProcedure;
DropDownList ddlTenant = (DropDownList)dvRoomCheckIn.FindControl("ddlTenant");
if (ddlTenant != null) cmd.Parameters.Add("#StudentID", SqlDbType.Char).Value = ddlTenant.SelectedValue.ToString();
cmd.Parameters.Add("#TenantID", SqlDbType.VarChar).Value = string.Empty;
DropDownList ddlBed = (DropDownList)dvRoomCheckIn.FindControl("ddlBeds");
if (ddlBed != null) cmd.Parameters.Add("#BedID", SqlDbType.Int).Value = ddlBed.SelectedValue.ToString();
cmd.Parameters.Add("#BoardingRoomID", SqlDbType.Int).Value = lblSelectedRoomID.Text;
cmd.Parameters.Add("#BoardingTypeID", SqlDbType.Int).Value = 2; //Boarder types only at this stage
cmd.Parameters.Add("#StaffID", SqlDbType.VarChar).Value = (Session["StaffID"] != string.Empty) ? Session["StaffID"].ToString() : "0000000001"; //default to Admin if null
TextBox txtCheckOUT = (TextBox)dvRoomCheckIn.FindControl("txtCheckOUT");
if (txtCheckOUT != null) cmd.Parameters.Add("#ExpectedCheckOut", SqlDbType.DateTime).Value = DateTime.Parse(txtCheckOUT.Text);
TextBox txtNotes = (TextBox)dvRoomCheckIn.FindControl("txtNotes");
if (txtNotes != null) cmd.Parameters.Add("#Notes", SqlDbType.VarChar).Value = txtNotes.Text;
//run query
int tst = cmd.ExecuteNonQuery();
if (tst != 1) throw new Exception("The Insert command did not work! tst=" + tst.ToString() + " ");
}
}
catch (Exception ex)
{
lblMsg.Text = ex.Message + " - " + ex.StackTrace;
}
//re-enable main panel and hide details panel
pnlRoomDetails.Visible = false;
pnlMAIN.Enabled = true;
gvCurrCheckIns.DataBind();
//update the motel layout
MotelFormat();
}
}
The problem is that when ever I SUBMIT a new tenant into the GridView gvAllocations, sometimes the server does a PostBack, other times it does nothing and the page is refreshed without the new entry shown in the Gridview. However, in the later case the page is refreshed properly after about 15-20 seconds and I can see the new tenant added into the GridView. Other times, it happens instantly.
I've tested the SQL server side-by-side with this, and the INSERT is working and I can see the new row immediately, but for some reason the ASP.net page does not register this, but only after 15-20 seconds. And as I said, this happens some of the time, which is even more frustrating.
Strangely, when ever I delete a tenant from the gvAllocations GridView, the whole thing works perfectly.
I am not sure if it's the UpdatePanel the issue here, or that this is perhaps poor design on my part and I should have used a FormView for the entire thing. Can you have FormViews inside UpdatePanels? Do I even need an UpdatePanel?
I've read this, and putting the ScriptManager RegisterPostBackControl() did not change anything for me.
Incidentally, the SUBMIT button renders as follows ...
<input type="submit" name="ctl00$ContentPlaceHolder1$ButtonSubmit" value="Submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$ButtonSubmit", "", true, "vg1", "", false, false))" id="ctl00_ContentPlaceHolder1_ButtonSubmit">
The CANCEL button does not have the WebForm_DoPostBackWithOptions(). Why?
Thank you for your time.
Well it looks like no one could help me out here and I was about to place a Bounty, but I found the culprit!
Yep - you've guessed it - Parameter Sniffing! It took me 4 days to figure it out.
The dvRoomCheckIn DetailsView had three parameters pre-designed into the control by myself, as you can see in the above code - the parameters were DateCheckedIn, CheckedINby, and ExpectedCheckOut.
The Stored Procedure was like this ...
CREATE PROCEDURE [dbo].[spBoardingCheckIN]
#StudentID CHAR(10),
#TenantID VARCHAR(20) = NULL,
#BedID INT,
#BoardingRoomID INT,
#BoardingTypeID INT,
#StaffID VARCHAR(10),
#ExpectedCheckOut DATETIME,
#Notes VARCHAR(5000)
AS
BEGIN
INSERT INTO tblBoardingCheckins(StudentID,
TenantID,
BedID,
BoardingRoomID,
BoardingTypeID,
DateCheckedIn,
CheckedINby,
ExpectedCheckOut,
Notes,
IsActive)
VALUES (#StudentID,
#TenantID,
#BedID,
#BoardingRoomID,
#BoardingTypeID,
GETDATE(),
#StaffID,
CONVERT(DATETIME, #ExpectedCheckOut, 121),
#Notes,
1)
END
GO
So what happened was that since I did not specify those three parameters into the Stored Procedure (I've defaulted the values and only mentioned one of them), the ASP.net (or SQL server?) went to find the values for the other two parameters that I mentioned in the DetailsView.
The way I fixed it was to specify all the Parameters that ASP.net expected, and therefore I changed the Stored Procedure and the parameter list in ASP.net to reflect the exact number of parameters expected, so as to avoid the program going into "sniffing" mode for those values.
ie
CREATE PROCEDURE [dbo].[spBoardingCheckIN]
#StudentID CHAR(10),
#TenantID VARCHAR(20) = NULL,
#BedID INT,
#BoardingRoomID INT,
#BoardingTypeID INT,
#DateCheckedIn DATETIME,
#CheckedINby VARCHAR(10),
#ExpectedCheckOut DATETIME,
#Notes VARCHAR(5000)
AS
BEGIN
INSERT INTO tblBoardingCheckins(StudentID,
TenantID,
BedID,
BoardingRoomID,
BoardingTypeID,
DateCheckedIn,
CheckedINby,
ExpectedCheckOut,
Notes,
IsActive)
VALUES (#StudentID,
#TenantID,
#BedID,
#BoardingRoomID,
#BoardingTypeID,
#DateCheckedIn,
#CheckedINby,
CONVERT(DATETIME, #ExpectedCheckOut, 121),
#Notes,
1)
END
GO
And the SqlDataSource in ASP.net is thus ...
<asp:SqlDataSource ID="sdsCheckinEntries" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
InsertCommand="spBoardingCheckIN" InsertCommandType="StoredProcedure" EnableCaching="false">
<InsertParameters>
<asp:ControlParameter ControlID="dvRoomCheckIn$ddlTenant" Name="StudentID" Type="string" />
<asp:Parameter Name="TenantID" Type="string" DefaultValue="" ConvertEmptyStringToNull="true" />
<asp:ControlParameter ControlID="dvRoomCheckIn$ddlBeds" Name="BedID" Type="int16" />
<asp:ControlParameter ControlID="lblSelectedRoomID" Name="BoardingRoomID" Type="Int16" />
<asp:Parameter Name="BoardingTypeID" Type="int16" DefaultValue="2" />
<asp:ControlParameter ControlID="dvRoomCheckIn$lblCheckedIN" Name="DateCheckedIn" Type="DateTime" />
<asp:ControlParameter ControlID="dvRoomCheckIn$lblCheckedINby" Name="CheckedINby" Type="string" />
<asp:ControlParameter ControlID="dvRoomCheckIn$txtCheckOUT" Name="ExpectedCheckOut" Type="DateTime" />
<asp:ControlParameter ControlID="dvRoomCheckIn$txtNotes" Name="Notes" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
So yeah lessoned learnt! Don't assume that just because a parameter exists in a control that it's automatically and directly linked to the datasource. Sometimes, the program may not "find it" and has to go looking for values in relation to the parameters missing.
A great resource - if not the best I've found so far is this. It explains everything.
I hope this will help someone else out there pulling their hair out!
Cheers

Adding asp table inside Templatefield

Here i have a problem with adding asp table inside the template field in gridview.
in my gridview one button is there named as "View" when i click this it should display all information below that row... so how do i do that??? should i use template field?? how to add button("View") for each row using this template field
Please correct my code
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:Table ID="tblModify" runat="server" Width="100%" Visible="False" CssClass="table">
<asp:TableRow>
<asp:TableCell ID="TableCell35" runat="server" HorizontalAlign="Right"><h1>Debit Information</h1></asp:TableCell>
<asp:TableCell></asp:TableCell>
<asp:TableCell></asp:TableCell>
<asp:TableCell></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow6" runat="server">
<asp:TableCell ID="TableCell3" runat="server" HorizontalAlign="Right">DebitNoteNumber</asp:TableCell><asp:TableCell
ID="TableCell4" runat="server">
<asp:Label ID="lblDebitNoteId" runat="server" MaxLength="50" Columns="70" />
</asp:TableCell><asp:TableCell ID="TableCell5" runat="server" HorizontalAlign="Right">Date</asp:TableCell><asp:TableCell
ID="TableCell6" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblDate" runat="server" MaxLength="30" Columns="50"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell ID="TableCell11" runat="server" HorizontalAlign="Right">Patient Name</asp:TableCell><asp:TableCell
ID="TableCell37" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblPatientName" runat="server" MaxLength="50" Columns="70"></asp:Label>
</asp:TableCell>
<asp:TableCell ID="TableCell1" runat="server" HorizontalAlign="Right">Patient Number</asp:TableCell><asp:TableCell
ID="TableCell2" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblPatientId" runat="server" MaxLength="50" Columns="70"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow2" runat="server">
<asp:TableCell ID="TableCell38" runat="server" HorizontalAlign="Right">Phone Number</asp:TableCell><asp:TableCell
ID="TableCell39" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblPatientPhoneNumber" runat="server"> </asp:Label>
</asp:TableCell>
<asp:TableCell ID="TableCell7" runat="server" HorizontalAlign="Right">Bill Number</asp:TableCell><asp:TableCell
ID="TableCell8" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblBillId" runat="server"> </asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow5" runat="server">
<asp:TableCell ID="TableCell9" runat="server" HorizontalAlign="Right">Receipt Number</asp:TableCell><asp:TableCell
ID="TableCell10" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblReceiptId" runat="server" MaxLength="30" Columns="50"></asp:Label>
</asp:TableCell><asp:TableCell ID="TableCell13" runat="server" HorizontalAlign="Right">Amount</asp:TableCell><asp:TableCell
ID="TableCell14" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblAmount" runat="server" MaxLength="30" Columns="50"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow8" runat="server">
<asp:TableCell ID="TableCell15" runat="server" HorizontalAlign="Right">Balacne</asp:TableCell><asp:TableCell
ID="TableCell16" runat="server" HorizontalAlign="Left">
<asp:Label ID="lblBalacne" runat="server" MaxLength="30" Columns="50"></asp:Label></td>
</asp:TableCell><asp:TableCell></asp:TableCell><asp:TableCell></asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:TemplateField>
if i use this code, i could't find button("View") in my gridview...
U can use Modal Pop Up Extender.
write simple html table tag(TR,TD) with your server side tag into the modal pop up.When You click on "View" the details will be shown into Modal pop up.