How to use a buttonfield in a gridview - asp.net

I am using this gridview:
<asp:GridView ID="gvMessages" runat="server" AutoGenerateColumns = "false"
CaptionAlign="NotSet" CellPadding="5" onrowcommand="gvMessages_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Avändare">
<ItemTemplate>
<%# GetSender((int)Eval("Sender"))%>
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ämne">
<ItemTemplate>
<%# Eval("Head")%>
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Öppna" CommandName="Open" />
</Columns>
</asp:GridView>
I want a textbox to show the message member of the bound object (Eval("Message")) of the row that has been clicked.

from the comments, and I would suggest to have the show/hide in the client, so you can save a round trip to the server
<asp:GridView ID="gvMessages" runat="server" AutoGenerateColumns = "false"
CaptionAlign="NotSet" CellPadding="5">
<Columns>
<asp:TemplateField HeaderText="Avändare">
<ItemTemplate>
<%# GetSender((int)Eval("Sender"))%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Ämne" DataField="Head" />
<asp:TemplateField HeaderText="Avändare">
<ItemTemplate>
<button class="btn-showmsg">Öppna</button>
<div class="message hide">
<asp:TextBox runat="server"
TextMode="MultiLine" Text="<%= Eval("Message") %>" />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and in javascript
$(function() {
$(".btn-showmsg").click(function() {
var tr = $(this).closest("tr"), // the <tr>
msg = tr.find(".message"); // the div wraping the message
msg.show(); // show it
});
});
Now, the position and the placement of the message, it's all up to you, but I would suggest something like Bootstrap Modal to show it.
also, remember to add a style of .hide { display: none; }

Related

focus a div on link button click

In my page, I have a gridview with link buttons in a column.
By clicking the link button should show a div below the gridview.
I gave href in onClientClick event of the link button as follows.
function showDiv() {
location.href = '#div1';
}
When I click the link button the div is showing, but after the page load the page goes up.
aspx code
<asp:GridView ID="gridDate" runat="server" CssClass="gridview_Order"
HeaderStyle-BackColor="#09182F" HeaderStyle-ForeColor="#ffffff"
AutoGenerateColumns="false" Visible="False">
<Columns>
<asp:TemplateField ItemStyle-Width="20%" HeaderText="Sl No." ItemStyle-CssClass="paddng">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="DATE" ItemStyle-CssClass="paddng">
<ItemTemplate>
<asp:LinkButton ID="lbtnDate" runat="server" CommandArgument='<%#Eval("tblName") %>' Text='<%# Eval("dtvar") %>' OnClientClick="showDiv()" OnCommand="lbtnDate_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="" ItemStyle-CssClass="paddng">
<ItemTemplate>
<asp:LinkButton ID="lbtnDownload" runat="server" Text="Download Excel" CommandArgument='<%# Eval("tblName") %>' OnCommand="lbtnDownload_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><hr /><br />
<div id="div1">
<asp:GridView ID="gridOrderByUser" runat="server" CssClass="gridview"
HeaderStyle-BackColor="#09182F" HeaderStyle-ForeColor="#ffffff"
AutoGenerateColumns="false" Visible="False" >
<Columns>
<asp:TemplateField ItemStyle-Width="2%" HeaderText="Sl No.">
<ItemTemplate>
<asp:Label ID="lblSlNo1" runat="server" Text='<%#Container.DataItemIndex+1 %>' style="color:#000;"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="cod" HeaderText="CODE" InsertVisible="False" ReadOnly="True" SortExpression="cod" ItemStyle-Width="16%" />
<asp:TemplateField HeaderText="IMAGE" ItemStyle-Width="18%">
<ItemTemplate>
<asp:Image ID="img1" runat="server" Height="100px" Width="54px" ImageUrl='<%#"~/images/"+Eval("Image") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
So what should I do for staying the page in the div position.
Thanks
If you dont need the server side event of the link button you could do this:
OnClientClick="location.href = '#div1'; return false;"

Selectively hiding a HyperLinkField in a gridview in a control

I have a control which I can call to populate the top of a aspx page with demographic information as follows.
<div runat="server" id="headerline" style="width:100%; background-color:#FFFFCC; border-color: #FFFFCC; " >
<asp:GridView ID="GridView1" skinID="headerline" runat="server" DataSourceID="odsPatientByID" AutoGenerateColumns="False" Width="100%" >
<HeaderStyle CssClass="invisible" />
<RowStyle Width="100px" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="PATIENT_ID" DataNavigateUrlFormatString="~/demographics/search_demographics.aspx?PatientID={0}" Text="DEMOGRAPHICS " ItemStyle-HorizontalAlign="Left" >
</asp:HyperLinkField>
<ItemTemplate >
<asp:Label SkinID="headerline" ID="lblNAME" runat="server" Text=' Name: ' />
<asp:Label SkinID="headerline" ID="lblPTNAME" runat="server" Text='<%# Shis.SCR.UI.Common.CapitalisePatientName(Eval("PT_NAME")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate >
<asp:Label SkinID="headerline" ID="lblDoB" runat="server" Text=' DoB: ' />
<asp:Label SkinID="headerline" ID="lblDateofBirth" runat="server" Text='<%# Bind("N1_10_DATE_BIRTH") %>' />
<asp:Label skinID="headerlineage" ID="lblAge" runat="server" Text='<%# GetDisplayAge("" & Eval("N1_10_DATE_BIRTH"),"" & Eval("L_DEATH_STATUS")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate >
<asp:Label SkinID="headerline" ID="lblSex" runat="server" Text=' Gender: ' />
<asp:Label SkinID="headerline" ID="lblGender" runat="server" Text='<%# EVAL("GENDER") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
I display the demographic data like this and it works fine
<div>
<controls:Demoline id="demoLine1" runat="server" />
</div>
What I would like to do is hide the HyperLinkfield when I select a Print option on the page and I get taken to the print page. Is this possible and if so how?
Look into RowDataBound event for the grid, it fires every time a row is bound to the grid. Do you check for a print param, from querystring or where ever and hide hyperlink. On phone so don't have code to hand
Look at
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.110).aspx
Give your Hyperlink an id eg "MyHyperLink"
add this code:
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (Request.QueryString["print"] != "1")
return;
var link = e.Row.FindControl("MyHyperLink") as HyperLink;
if (link != null)
link.Visible = false;
}
and then my add to your asp:GridView element:
OnRowDataBound="GridView1_OnRowDataBound"

What property of a Templatefield will make an asp button visible?

I have the following code on my aspx page that has an onclick event to produce a popup control but the button is invisible. What can I use in place of ItemTemplate as a property of Templatefield to make the button visible?
<asp:TemplateField HeaderText="Performance Status (adults):">
<ItemTemplate>
<asp:button skinID="tabbutton" CssClass="Clickable" id="btnWhoStatus" runat="server" Text="?" BorderStyle="None" TabIndex="-1" />
<cc1:PopupControlExtender
ID="PopupControlExtender2" runat="server" Position="Top"
TargetControlID="btnWhoStatus" PopupControlID="pnlWhoStatus"
DynamicServicePath="" Enabled="True" ExtenderControlID="">
</cc1:PopupControlExtender>
<asp:Panel runat="server" ID="pnlWhoStatus" BackColor="#FFFFFF" style="width:640px; display:none " >
<controls:WhoStatusPopup id="WhoStatusPopup" runat="server" />
</asp:Panel>
</ItemTemplate>
<ItemTemplate>
<asp:Label skinid="tablabelblack" id="lblPerformanceStatus" runat="server" Text='<%# Eval("PerformanceStatus") %>' />
</ItemTemplate>
</asp:TemplateField>

Assign value of nested gridview into CommandName

I have a nested gridview and I want to get the value of the first gridview and assign it to the commandArgument of a button. Can someone advise how to do it
As you will see in the code below I'm already getting the row index with the following code:
"
Text="Save Changes" />
What I need is the DataField="name" instead
See full code below.
<script language="javascript" type="text/javascript">
function divexpandcollapse(divname) {
var div = document.getElementById(divname);
var img = document.getElementById('img' + divname);
if (div.style.display == "none") {
div.style.display = "block"; img.src = "Images/Icons/minus.jpg";
} else { div.style.display = "none"; img.src = "Images/Icons/plus.jpg"; }
}</script>
<asp:DropDownList ID="DateSelection" runat="server" AutoPostBack="True" Height="21px"
Width="134px">
</asp:DropDownList>
<asp:GridView ID="GV_SL" runat="server" OnRowDataBound="gvUserInfo_RowDataBound"
OnRowCommand="GV_SL_RowCommand" AutoGenerateColumns="False"
DataSourceID="SQL_Weekly" AllowSorting="True">
<Columns>
<asp:TemplateField ItemStyle-Width="50px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("name") %>');">
<img id="imgdiv<%# Eval("name") %>" width="15px" border="0" src="Images/Icons/plus.jpg" /></a></ItemTemplate>
<ItemStyle Width="40px" />
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="Group" SortExpression="name" />
<asp:BoundField DataField="ASL" HeaderText="SL% Act" ReadOnly="True" SortExpression="ASL" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("name") %>" style="display: none; position: relative; left: 15px;
overflow: auto">
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Metric" HeaderText=" " HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Actual" HeaderText="Actual" HeaderStyle-HorizontalAlign="Left" />
</Columns>
</asp:GridView>
<br />
<asp:UpdatePanel ID="UP_SecondPanel" runat="server" UpdateMode="Always" >
<ContentTemplate>
<%-- --%>
<asp:TextBox ID="TB_Comments" runat="server" Text="Example: Text will be entered here"
TextMode="MultiLine" Rows="4" Width="510px"></asp:TextBox>
<asp:Button ID="B_Save" runat="server" CommandName="AddText" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
Text="Save Changes" />
</ContentTemplate>
</asp:UpdatePanel>
<%-- --%>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Any ideas will be appreceiated
Thanks
I sorted the problem with VB code. See below if it helps someone
Protected Sub GV_SL_RowCommand(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "AddText") Then
' Retrieve the row index stored in the CommandArgument property.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button
' from the Rows collection.
Dim row As GridViewRow = GV_SL.Rows(index)
Dim GroupName As String = GV_SL.Rows(index).Cells(1).Text
'Rest of the code here
End If
End Sub

How do i get asp:Panel Default Button to fire button click event?

I have three asp net text boxes that will contain search criteria for searching a table. I would like the search to work like Google. When the enter key is pressed to have the click event on the "Enter" button fire. The click event never fires. What can I do to get it to fire?
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebCategoryGenerateEdit.ascx.cs"
Inherits="EPPA.Controls.WebCategoryGenerateEdit" ClientIDMode="Static" %>
<script src="/Scripts/jquery-1.8.3.js" type="text/javascript"></script>
<script type="text/javascript">
// debugger;
function pageLoad() {
var gvCategoryMessage = $("#<%=GVCategoryMessage.ClientID %>").html
if (gvCategoryMessage.length != 0) {
$("#<%=GVParentCategoryLinkTitle.ClientID %>").keydown(function () {
$("#<%=GVCategoryMessage.ClientID %>").html("");
})
$("#<%=GVCategoryLinkTitle.ClientID %>").keydown(function () {
$("#<%=GVCategoryMessage.ClientID %>").html("");
})
}
}
</script>
<asp:ScriptManager runat="server" />
<asp:MultiView ID="Multiview1" runat="server" ActiveViewIndex="0">
<asp:View ID="SearchView" runat="server">
<asp:Table ID="TableSV1" runat="server">
<asp:TableRow ID="TableRowSV1" runat="server">
<asp:TableCell ID="TableCellSV1" runat="server">
<asp:Label ID="LabelSV1" runat="server" SkinID="SearchTextLabel"
Text="CB or CJ" CssClass="searchTextLabel_Division"/>
</asp:TableCell>
<asp:TableCell ID="TableCellSV2" runat="server">
<asp:Label ID="LabelSV2" runat="server"
SkinID="SearchTextLabel" Text="Parent Category Link Title"
CssClass="searchTextLabel_ParentCategoryLink" />
</asp:TableCell>
<asp:TableCell ID="TableCellSV3" runat="server">
<asp:Label ID="LabelSV3" runat="server"
SkinID="SearchTextLabel"
Text="Category Link Title" CssClass="searchTextLabel_CategoryLink" />
</asp:TableCell>
<asp:TableCell ID="TableCellSV4" runat="server">
<asp:Label ID="Label1" runat="server"
SkinID="SearchTextLabel"
Text="Category ID" CssClass="searchTextLabel_CategoryID" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="SVTableRowSV2" runat="server">
<asp:TableCell ID="TableCellSV5" runat="server">
<asp:DropDownList ID="SVDivisionDDL" runat="server"
OnSelectedIndexChanged="SVDivisionDDL_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Value="CB">CB</asp:ListItem>
<asp:ListItem Value="CJ">CJ</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
<asp:TableCell ID="TableCellSV6" runat="server" >
<asp:panel ID="SVPCLTContainer" runat="server"
DefaultButton="SV6EnterButton">
<asp:TextBox ID="SVParentCategoryLinkTitle" runat="server"
SkinID="SearchTextBox"
CssClass="searchTextBox_ParentCategoryLink" />
<asp:Button ID="SV6EnterButton" runat="server" Visible="false"
Text="Enter" OnClick="EnterKeyPressed_Click" />
</asp:panel>
</asp:TableCell>
<asp:TableCell ID="TableCellSV7" runat="server" >
<asp:TextBox ID="SVCategoryLinkTitle"
runat="server" SkinID="SearchTextBox"
CssClass="searchTextBox_CategoryLink" />
</asp:TableCell>
<asp:TableCell ID="TableCellSV8" runat="server" >
<asp:TextBox ID="SVCategoryID" runat="server"
SkinID="SearchTextBox"
CssClass="searchTextBox_CategoryID" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="SVGenNewButton" runat="server" Text="Generate New Category ID"
OnClick="SVGenerateNewButton_Click" />
<asp:Button ID="SVDeleteButton" runat="server" Text="Delete Checked items"
OnClick="SVDeleteButton_Click" />
<div style="width:618px; overflow-x:no-display; overflow-y:auto; max-height:392px;
padding-top:2px; ">
<asp:GridView ID="gvWebCategoryID" runat="server" AutoGenerateColumns="False"
SkinID="gridViewSkin" OnRowDeleting="gvWebCategoryID_RowDeleting" >
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="SVgridViewEditButton" runat="server"
OnClick="gvWeCategoryIDEditButton_Click" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox ID="Checkbox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Division" HeaderText="CB or CJ"
Visible="true" />
<asp:BoundField DataField="ParentCategoryLinkTitle" HeaderText="Parent
Category Link Title" />
<asp:BoundField DataField="CategoryLinkTitle" HeaderText="Category
Link Title" />
<asp:BoundField DataField="CategoryID" HeaderText="Category ID" />
<asp:TemplateField HeaderText="Error Message" Visible="false" >
<ItemTemplate>
<asp:Label ID="ErrorMessage" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:View>
</asp:MultiView>
and here is the behind code method:
protected void EnterKeyPressed_Click(object sender, EventArgs e)
{
ReloadData();
this.Multiview1.ActiveViewIndex = 0;
}
At a quick glance, have you tried removing the return false here:
$("#<%=SVParentCategoryLinkTitle.ClientID %>").keydown(function (e) {
if (e.which == 13) {
__doPostBack("#<%=EnterKeyPressed.UniqueID %>", "");
return false;
}
})
check out this question and answer, if it helps: Default button not working in asp.net panel
the DefaultButton is used to capture the Enter key press if the focus is already inside the panel, for example you must have moved the focus to an input field like the text boxes inside the panel. if the focus is not there it won't work.

Resources