i have two gridviews 1) master and 2) detail in my master gridview with few columns in it and a hyperlink so when the user click on the hyperlink (master gridview) i want the row to be highlight but below codes does not hold the highlighted row after it does the postback, how do i make sure that its highlight even after it does postback?
protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#C2D69B'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
e.Row.Attributes.Add("style", "cursor:pointer;");
//e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
}
}
<asp:GridView runat="server" ID="gvReport" AutoGenerateColumns="False" CssClass="gv"
DataSourceID="LDS_POReport" Width="880px" AllowPaging="true" AllowSorting="true"
OnRowCreated="gvReport_RowCreated" OnRowDataBound="gvReport_RowDataBound" DataKeyNames="Id" PageSize="15">
<PagerStyle HorizontalAlign="Left" CssClass='header' BackColor="#E5EAF3" ForeColor="Black" />
<PagerSettings Mode="NumericFirstLast" />
<EmptyDataTemplate>
No Items</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number Of Items" SortExpression="NumberOfItems">
<ItemTemplate>
<a href='Officer.aspx?Id=<%# Eval("Id") %>'>
<%# Eval("NumberOfItem")%>
</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
sorry for question... are you sure that you check between 2 integer values?... and if yes...
in the html that you have, do you have the background color in the TD elements of the table? If you have the color code in the html code, maybe it's a problem of css style definition.
Just put a row index in the query string and read it after the postback, then select the row based on index and change the color.
Related
I have a GridView with various fields, one of which I have used TemplateFields for in order to have a datepicker in it. I've set the GridView to be editable and for all the other fields, this works great and they stay locked until the edit button is pressed.
Unfortunately, the TemplateField stays editable at all times. I want them to stay readonly until 'Edit' is clicked.
Some code to illustrate what I'm doing.
default.aspx
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
AllowSorting="True" AutoGenerateEditButton="True"
OnPreRender="GridView1_PreRender"
CssClass="gvdatatablem, table table-striped table-bordered"
DataKeyNames="fmatter" PageSize="25">
<Columns>
<asp:TemplateField HeaderText="Letter Sent">
<ItemTemplate>
<asp:TextBox ID="udtltrsent" runat="server" ReadOnly="false" Class='datepicker' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="uheld" HeaderText="Held"
SortExpression="uheld" />
<asp:BoundField DataField="udtresponse" HeaderText="Response"
SortExpression="udtresponse" />
<asp:BoundField DataField="clname1" HeaderText="clname1"
SortExpression="clname1" readonly="true" />
</Columns>
</asp:GridView>
Javascript at the bottom to implement Datatables and Bootstrap datepicker
<script type="text/javascript">
$(document).ready(function () {
$('#example').DataTable();
$('.datepicker').datepicker();
});
</script>
Also the GridView1_PreRender method is just to change the table format, for Datatables to work
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.Rows.Count > 0)
{
//Replace the <td> with <th> and adds the scope attribute
GridView1.UseAccessibleHeader = true;
//Adds the <thead> and <tbody> elements required for DataTables to work
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
//Adds the <tfoot> element required for DataTables to work
//GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
}
The solution worked for me is Use label in ItemTemplate and Keep EditItemTemplate for Textboxes. After Edit get that textbox date to label text.
<Columns>
<asp:TemplateField HeaderText="Letter Sent">
<ItemTemplate>
<asp:Label ID="lbldate" runat="server" ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="udtltrsent" runat="server" Class='datepicker' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
I have a Gridview with a column that has a DropDownList.
I've binded this Dropdownlist with an event on the "SelectedIndexChanged".
The problem is i can't get the value of a label of another column in the same row.
The code is the next:
protected void grid_OnSelectedIndexChanged(object sender, EventArgs e)
{
grdCredenciales.DataBind();
var dropdown = (DropDownList)sender;
var row = (GridViewRow)dropdown.NamingContainer;
var label = (Label)row.FindControl("lblMatricula");
var value = label.Text; // I get "" in this line.
}
And in the grid i have:
<asp:ObjectDataSource ID="CredencialesDS" runat="server" />
<asp:GridView ID="grdCredenciales" runat="server" BackColor="White" DataSourceID="CredencialesDS"
CssClass="DDGridView" RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6" AllowSorting="True"
AllowPaging="True" AutoGenerateColumns="False" PageSize="10" OnRowDataBound="grdCredenciales_OnRowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label7" ToolTip="MatrĂcula" runat="server" Text="MatrĂcula"/>
</HeaderTemplate>
<HeaderStyle HorizontalAlign="Left" Width="15%"/>
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblMatricula" runat="server"><%# Eval("Matricula") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label19" ToolTip="Estado" runat="server" Text="Estado" />
</HeaderTemplate>
<HeaderStyle HorizontalAlign="Left" Width="15%"/>
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:DropDownList runat="server" ID="dpEstadoCredencial" AutoPostBack="True" OnSelectedIndexChanged="grid_OnSelectedIndexChanged" CssClass="comboEstado"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I don't know why, but label.text returns an empty string. As you can see, i am calling the DataBind before, so the label should have a value at this point.
Do you know how can i get the value i need from the label in another column?
Thanks for everyone.
Check the GridView's DataSource before you do the DataBind(). Since you're missing the full ASPX markup, I'm not sure if you're setting the data source programmatically or with a SqlDataSource.
In any case, what will happen often with programmatically-set Data Sources is that they disappear on a PostBack, and when you call that DataBind, you're really DataBinding it to null, which would explain why you're getting string.Empty ("") for the Label's Text property.
Just verified the code provided by you. It's working completely.
Please make sure in the RowDataBound event of Grid View, you reattach the dropdownlist's SelectedIndexChanged event as below:
protected void CustomersGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (Page.IsPostBack)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("dropdown1") as DropDownList;
if (ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(CustomersGridView_SelectedIndexChanged);
}
}
}
}
Also, I used the same code as yours in SelectedIndexChanged event. I'm putting here my aspx Markup:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
runat="server"
OnRowDataBound="CustomersGridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="Label2" Text='<%# Bind("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="dropdown1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdown1_SelectedIndexChanged">
<asp:ListItem Text="Cat"></asp:ListItem>
<asp:ListItem Text="dog"></asp:ListItem>
<asp:ListItem Text="Mouse"></asp:ListItem>
<asp:ListItem Text="pig"></asp:ListItem>
<asp:ListItem Text="snake"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
Please provide your GridView markup too for checking.
I have a gridview with a linkbutton that is posting crosspage when a value in the ID column is clicked. I want the ID to be posted to the next page so that it cant be seen in the url. (no querystring) The gridview is contained within a contentplaceholder. I would like to know how to response.write the linkbuttons (lbID) text on details.aspx using findcontrol. Please help. (Im using VB, I should have mentioned that.)
<asp:Content ID="content" ContentPlaceHolderID="content" runat="server">
<asp:GridView ID="gvOpen" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="Black" BorderStyle="None" BorderWidth="1px"
CellPadding="4"
ForeColor="Black" Width="96%" DataKeyNames="id"
DataSourceID="Open" CssClass="Grid" AllowPaging="True">
<AlternatingRowStyle BackColor="#CCFFCC" />
<Columns>
<asp:ImageField DataImageUrlField="priority" HeaderText="Priority">
<ItemStyle Height="28px" HorizontalAlign="Center" VerticalAlign="Middle" Width="28px" />
</asp:ImageField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:LinkButton ID="lbID" runat="server" PostBackUrl="~/details.aspx"> <%# Eval("ID") %></asp:LinkButton>
</ItemTemplate>
I think this is what you want: PostBackUrl
void Page_Load(object sender, EventArgs e)
{
LinkButton lbID = (LinkButton) PreviousPage.FindControl("lbID");
string linkText = "";
if(lbID != null)
linkText = lbID.Text;
}
You might want to set linkButton text like this
<asp:LinkButton ID="lbID" runat="server" PostBackUrl="~/details.aspx" Text='<%# Eval("ID") %>'></asp:LinkButton>
I have a ModalPopupExtender which is, among other things, populated with a gridview (From a DataTable).
In this GridView I have a deletebutton attached to each row, which is supposed to delete the row. Is it anyway possible to delete the selected row from the datatable, and then update the GridView without closing the ModalPopupExtender?
Here is my GridView:
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupDragHandleControlID="divPopupReport" TargetControlID="btnHidden" PopupControlID="divPopupReport" CancelControlID="btnCloseReport" BackgroundCssClass="modalBackground"></ajaxToolkit:ModalPopupExtender>
<asp:UpdatePanel runat="server" ID="upReport">
<ContentTemplate>
<div id="divPopupReport" runat="server" style="text-align:left; padding-right:0px; background-color:White; border: 2px solid #87d000; display:none;" >
<asp:GridView ID="GridView2" runat="server" CssClass="list listExtended"
DataKeyNames="DocumentGuid" Width="100%" OnRowCommand="GridView2_RowCommand" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="DocumentName" HeaderText="Dokumentname">
<ItemStyle CssClass="list"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="CardName" HeaderText="Reference">
<ItemStyle CssClass="list"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="DocumentDate" HeaderText="Date">
<ItemStyle CssClass="list"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="">
<ItemStyle CssClass="list" />
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemStyle CssClass="list" />
<ItemTemplate>
<asp:ImageButton ID="btnDelete" CssClass="image" runat="server" CommandName="Delete" CommandArgument='<%# Eval("DocumentGuid") %>' ImageUrl="~/delete.gif" Width="16" Height="16" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
And below my RowCommand.
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
DataTable SelectedDataTable = Session["SelectedDataTable"] as DataTable;
string guid = Convert.ToString(e.CommandArgument);
DataRow[] dr = SelectedDataTable.Select("DocumentGuid = '" + guid + "'");
SelectedDataTable.Rows.Remove(dr[0]);
Session["SelectedDataTable"] = SelectedDataTable;
GridView2.DataSource = SelectedDataTable;
GridView2.DataBind();
}
}
You need to wrap an UpdatePanel around the grid. This should solve the problem.
Another option is to use ajax.
Using Javascript/JQuery to remove the table row manually in combination with a WebMethod:
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Another easy solution is to re-show the popup after post back.
Swap upReport panel with divPopupReport div (i.e. place upReport UpdatePanel into divPopupReport div).
I have a GridView in an update panel.
The GridView is styled so that each alternate row is a different colour.
When I change page on the GridView it loses the alternate row colouring. All other styles are maintained.
If I remove the update panel the GridView keeps the alternate row colouring after changing page.
Does anyone have any idea on what may be causing this or how to fix it?
Thanks,
Neil
EDIT:
Here is the aspx code
<div id="active-logbooks" class="tab-content clearfix">
<div class="left-column">
<asp:MultiView runat="server" ID="mlvLogbooks" >
<asp:View runat="server" ID="vActiveLogbooks">
<asp:GridView PagerSettings-Mode="NextPrevious" PagerSettings-Position="Top" PagerSettings-NextPageImageUrl="~/img/right-arrow.png" PagerSettings-PreviousPageImageUrl="~/img/left-arrow.png" AllowPaging="true" runat="server" ID="gvActiveLogbooks" PageSize="5" AutoGenerateColumns="false" CssClass="lesson stripe-me" OnRowDataBound="gvActiveLogbooks_RowDataBound" OnPageIndexChanging="gvActiveLogbooks_PageIndexChanging">
<Columns>
<asp:BoundField HeaderText="Logbook number" DataField="LogbookNumber" ItemStyle-CssClass="border" ItemStyle-Width="100" />
<asp:BoundField HeaderText="Origin" DataField="Origin" ItemStyle-CssClass="border" ItemStyle-Width="100" />
<asp:BoundField HeaderText="Order Reference" DataField="OrderReference" ItemStyle-CssClass="border" ItemStyle-Width="100" />
<asp:TemplateField HeaderText="Transfer Date">
<ItemTemplate>
<asp:Literal runat="server" ID="lblTransferDate" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" ID="lnkTransferLogbook" CssClass="border" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Previous<asp:Literal runat="server" ID="litBreaker" Text=" |" />
Next
</asp:View>
</asp:MultiView>
</div>
And this is the function that is called when the page change occurs:
protected void gvActiveLogbooks_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
HideShowGridViewPagerLinks(e.NewPageIndex);
gvActiveLogbooks.DataSource = _logbooks;
gvActiveLogbooks.PageIndex = e.NewPageIndex;
gvActiveLogbooks.DataBind();
}
Nowhere on the page is there anything done with colouring the GridView rows
protected void gvActiveLogbooks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
Literal transferLabel = (Literal)e.Row.Cells[(int)ActiveLogbookGridViewColumns.TransferDate].FindControl("lblTransferDate");
transferLabel.Text = _logbooks[e.Row.RowIndex].TransferDate.ToShortDateString();
HyperLink transferLink = (HyperLink)e.Row.Cells[(int)ActiveLogbookGridViewColumns.TransferLink].FindControl("lnkTransferLogbook");
transferLink.Text = TransferLinkText;
transferLink.NavigateUrl = "TransferLogbooks.aspx?id=" + Guid.NewGuid();
}
I'm guessing that some interaction between your CSS classes and the postback is messing this up. What happens if you switch to the RowStyle and AlternateRowStyle tags (example here), and reference your CSS classes using the CssClass property on those tags?