How to collapse and expand devexpress master/detail asp.net gridview - asp.net

I have two asp.net devexpress gridviews in master/detail configuration. I expand a master row to display the child rows and do manipulations on the child rows such as edit, add new and delete rows. After that I want the parent gridview to collapse and both the gridviews to refresh with new data. Please let me know how could I do it. On devexpress site I see the mention of CollapseRow and ExpandRow javascript clientside methods. But couldn't find any sample code describing how to call those.
Thanks

First assign clientInstanceName to grid as:
<dx:ASPxGridView ID="ASPxGridView1" runat="server" KeyFieldName="ID"
ClientInstanceName="grid">
When you complete update or delete as you said call grid.CollapseAllDetailRows(); after the operation that you have completed. or on ASPxClientGridView.DetailRowExpanding Event set the expanded detail row visibleindex. check the following code snippet and code as per your requirement.
<html>
<head runat="server">
<title></title>
<script language ="javascript" type ="text/javascript">
var focusedIndex;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="ASPxGridView1" runat="server" KeyFieldName="ID" ClientInstanceName="grid">
<ClientSideEvents DetailRowExpanding="function(s, e) {
focusedIndex = e.visibleIndex;
}" />
<SettingsBehavior AllowFocusedRow="True" AllowSelectByRowClick = "true" />
<SettingsDetail ShowDetailRow="True" />
</dx:ASPxGridView>
<dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="ASPxButton">
<ClientSideEvents Click="function(s, e) {
//var visibleindex = grid.GetFocusedRowIndex();
//grid.CollapseAllDetailRows();
if( focusedIndex != 'undefined')
{
grid.CollapseDetailRow(focusedIndex );
}
}"/>
</dx:ASPxButton>
</div>
</form>
</body>
</html>

Related

aspx Gridview conditional format

I have a simple gridview and code behind to conditionally format some rows. It runs but nothing gets colored. I've tried various autoformatting and finally removed all in the hope that there was some magic but to no avail
protected void GridviewRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int CellValue = Convert.ToInt32(e.Row.Cells[2].Text);
if (CellValue >= 0)
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Green;
}
if (CellValue < 0)
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
}
<%# Page Language="VB" CodeBehind="TrainPurch.aspx.vb" %>
<!DOCTYPE html>
<html lang="en" xmlns=http://www.w3.org/1999/xhtml>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="D:\Inetpub/wwwroot\dealerinfo\oracledealerinfo.mdb" SelectCommand=" SELECT tblDealerMifTrained.R12Account, tblDealerMifTrained.GroupOwner, tblDealerMifTrained.Units AS Units, tblDealerMifTrained.ProductCode, ProdToEDP.Model, qry6aTrainedPurchase.Trained FROM (ProdToEDP INNER JOIN tblDealerMifTrained ON ProdToEDP.ProductCode = tblDealerMifTrained.ProductCode) INNER JOIN qry6aTrainedPurchase ON (tblDealerMifTrained.ProductCode = qry6aTrainedPurchase.ProductCode) AND (tblDealerMifTrained.R12Account=qry6aTrainedPurchase.R12Account) WHERE (((tblDealerMifTrained.Type)='Purchase')) GROUP BY tblDealerMifTrained.R12Account, tblDealerMifTrained.GroupOwner, tblDealerMifTrained.Units, tblDealerMifTrained.ProductCode, ProdToEDP.Model, qry6aTrainedPurchase.Trained;"></asp:AccessDataSource>
<div style="height:750px; overflow:auto">
<asp:GridView
id="GridView2"
runat="server"
AllowSorting="True"
DataSourceID="AccessDataSource1" Caption="Trained Purchased" PageSize="25" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="R12Account" HeaderText="R12Account" SortExpression="R12Account" />
<asp:BoundField DataField="GroupOwner" HeaderText="GroupOwner" SortExpression="GroupOwner" />
<asp:BoundField DataField="Units" HeaderText="Units" SortExpression="Units" />
<asp:BoundField DataField="ProductCode" HeaderText="ProductCode" SortExpression="ProductCode" />
<asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
<asp:BoundField DataField="Trained" HeaderText="Trained" SortExpression="Trained" />
</Columns>
</asp:GridView> </div>
</form>
</body>
</html>
Display the page in design mode, right click on the gridview, and display the property sheet.
Like this:
Now that you display the property sheet, eg this:
In above, click on the event icon (lighting bolt).
You now see this:
So, double click on the Row data bound text area, and that should jump you to your code behind editor, and you can now type in code for that event.
However, I do note that the page you have has the code behind set for VB, and not c#. So I would REALLY suggest that you create a blank new page, and then copy the parts of the old web form into the new one - don't copy the WHOLE page markup, but JUST the parts inside of the form tags that you require. And then try the above set of steps to create the row data bound event.
In your code RowDataBound event missing.
<asp:GridView id="GridView2" runat="server" AllowSorting="True" OnRowDataBound= "GridviewRowDataBound"

asp:listbox validation

I have two asp:ListBox. The ID's are Authors and AuthorsSelected. Authors listbox is loaded with all the authors and AuthorsSelected is empty at first. Using Javascript code, I am moving items from Authors to AuthorsSelected.
Now, before submitting the form, I want to verify that AuthorsSelected listbox is not empty. I tried asp:RequiredFieldValidator and it's not workijng and giving error message.
Please let me know how to validate the AuthorsSelected listbox and make sure it's not empty before submitting the form. Thanks.
Look at this very simple example:
<p>
<script language="javascript" type="text/javascript">
function validateListbox() {
var listbox = document.getElementById("<%= ListBox1.ClientID %>");
if (listbox.length == 0)
alert("no items!");
return (listbox.length > 0);
}
</script>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
</asp:ListBox>
</p>
<p>
<asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClientClick="return validateListbox()" />
</p>
Please note ListBox1.ClientID usage; I use it in order to reference my listbox in JavaScript.

Asp.Net and Shadowbox

Trying to pop up an IFrame Shadowbox with Jquery from an asp:gridview datarow. I can't get the proper quotes into the string:
<asp:ImageButton ID="btnEdit" runat="server"
OnClientClick='<%# "javascript:popAccount(\'"+
Eval("id", "Popup.aspx?id={0}")+"\');" %>' />
Parser Error Message: The server tag is not well formed.
Without the escaped single quotes (cannot work, but parses properly):
<asp:ImageButton ID="btnEdit" runat="server"
OnClientClick='<%# "javascript:popAccount("+
Eval("id", "Popup.aspx?id={0}")+");" %>' />
Client-side HTML, as expected:
onclick="javascript:popAccount(Popup.aspx?id=3ce3b19c-1899-4e1c-b3ce-55e5c02f1);"
How do I get quotes into the Javascript?
Edit: added solution. This not very generic, since the databound types must be known in order to access the id property. The key (as defined in the GrodView's DataKeyNames parameter) does not seem to be exposed in the event argument. But it works.
protected void editGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
// do not look at header or footer
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btn = e.Row.FindControl("btnPopup") as ImageButton;
if (btn != null)
{
btn.OnClientClick =
"javascript:popAccount('EditAccountPopup.aspx?"+
Constants.acctidParam+"="+
((tb_account)(e.Row.DataItem)).id.ToString()+"');";
}
}
Pick up the ImageButton in the GridView's rowdatabound event in the codebehind, and add the property from there.
But are you sure you need to use a server control? How about just a plain image? If you're "replacing" the click-behaviour of the ImageButton, it doesn't seem like you need it at all.
Update:
For the code-behind solution, I coded up this little sample (in VB.NET, sorry):
Private Sub gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridview1.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim btnEdit As ImageButton = CType(e.Row.Cells(0).FindControl("btnEdit"), ImageButton)
Dim strID As String = CType(e.Row.DataItem, Guid).ToString
btnEdit.Attributes.Add("onclick", String.Format("javascript:popAccount('Popup.aspx?id={0}');", strID))
End Select
End Sub
However, I'd still recommend you go with a simpler aproach, jQuery is excellent here. No
need for any code in your codebehind:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("img.edit").click(function() {
var sID = this.id.replace('btnEdit_', '');
alert(sID); // Add your popup opening logic here...
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gridview2" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img id="btnEdit_<%#Container.DataItem %>" class="edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
If you want to keep your code the way it is, why not just do this?
<asp:ImageButton ID="btnEdit" runat="server"
OnClientClick='<%# "javascript:popAccount(&quot"+
Eval("id", "Popup.aspx?id={0}")+"&quot);" %>' />

Refering to controls through javascript function

I have a radiobutton column in a gridview, inside many nested asp tables. I want radios enable two buttons upon click. so I added this the following function an javascript but it cannot find my controls although I turned ClientIDMode="Static" for those buttons. It returns null.
<asp:GridView ID="gridView_stLists" runat="server" AutoGenerateColumns="False" CellPadding="3"
BorderStyle="NotSet" CssClass="table_layout" Width="500">
<RowStyle CssClass="table_body" />
<Columns>
<asp:TemplateField HeaderStyle-Width="20">
<ItemTemplate>
<input name="radioBtn_res" type="radio" value='<%# Eval("uri") %>' onclick="rdBtn_onClick()" />
</ItemTemplate>
<script language="javascript" type="text/javascript">
function rdBtn_onClick()
{
document.getElementById("btn_delete_list").enable=true;
document.getElementById("btn_showRes").enable=true;
}
</script>
can the problem be from the place of script tag? I put it under content tag.
Change your JavaScript to this
<script language="javascript" type="text/javascript">
function rdBtn_onClick() {
document.getElementById("<%= btn_delete_list.ClientID %>").disabled = false;
document.getElementById("<%= btn_showRes.ClientID %>").disabled = false;
}
</script>
I'm guessing that the problem is to do with INamingContainer changing the ids of controls in the rendered HTML sent to the client.
Where are the buttons in your markup? What does the HTML look like when you View Source?
can the problem be from the place of script tag? I put it under content tag.
No.
Where are the buttons? View the generated html source. Are the buttons there with the id you are looking for?
Also, it should be element.disabled = false, not element.enable = true

Can I add an additional action to a DetailsView in ASP.NET?

Currently I have a DetailsView attached to a GridView. When you select an item from the GridView the details show up in the DetailsView (duh). Right now there are 3 event options on the DetailsView: Edit, Delete, and New. I would like to add a 4th, Format. This will simply run some queries and then perform a delete. Is this possible and how?
Another possibility would be to add this event to the GridView instead (maybe while in Edit mode?)
If you add your own buttons to the DetailsView as suggested you can hook the ItemCommand event on the DetailsView and add any logic for each CommandName in the ItemCommand method
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomerDetailView_ItemCommand(Object sender, DetailsViewCommandEventArgs e)
{
// Use the CommandName property to determine which button
// was clicked.
if (e.CommandName == "Add")
{
// Add the current store to the contact list.
// Get the row that contains the store name. In this
// example, the store name is in the second row (index 1)
// of the DetailsView control.
DetailsViewRow row = CustomerDetailView.Rows[1];
// Get the store's name from the appropriate cell.
// In this example, the store name is in the second cell
// (index 1) of the row.
String name = row.Cells[1].Text;
// Create a ListItem object with the store's name.
ListItem item = new ListItem(name);
// Add the ListItem object to the ListBox, if the
// item doesn't already exist.
if (!ContactListBox.Items.Contains(item))
{
ContactListBox.Items.Add(item);
}
}
}
</script>
<html >
<head runat="server">
<title>
DetailsView ItemCommand Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>
DetailsView ItemCommand Example</h3>
<asp:DetailsView ID="CustomerDetailView"
DataSourceID="DetailsViewSource"
AutoGenerateRows="false"
DataKeyNames="CustomerID"
AllowPaging="true"
OnItemCommand="CustomerDetailView_ItemCommand"
runat="server">
<FieldHeaderStyle BackColor="Navy" ForeColor="White" />
<Fields>
<asp:BoundField DataField="CustomerID" HeaderText="Store ID" />
<asp:BoundField DataField="CompanyName" HeaderText="Store Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:ButtonField CommandName="Add" Text="Add Contact" />
</Fields>
</asp:DetailsView>
<hr />
Contacts:<br />
<asp:ListBox ID="ContactListBox" runat="server" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the web.config file. -->
<asp:SqlDataSource ID="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (#CustomerID, #CompanyName, #Address, #City, #PostalCode, #Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>

Resources