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>
Related
I have a Telerik RadGrid inside an Update Panel. One of the columns in the RadGrid has link buttons. I would like to export an excel file when the button is clicked.
I have written the code and when I click on the linkButton, the page refreshes and the download does not happen. I have tried using anchor tag instead of the link button but it didnt work.
My ASP.net code is here
<telerik:RadAjaxPanel runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
<telerik:RadGrid ID="GridViewAllRequests" runat="server" CssClass="ms-listviewtable" border="0" BorderStyle="None" HeaderStyle-BorderStyle="None" CellPadding="4" GridLines="None" Width="100%"
AutoGenerateColumns="False" AllowFilteringByColumn="True" MasterTableView-ShowFooter="false" ShowStatusBar="false"
AllowPaging="True" PageSize="25" AllowSorting="true" MasterTableView-AllowMultiColumnSorting="true" EnableLinqExpressions="false"
ShowHeaderWhenEmpty="true" ShowFooter="false" DataSourceID="LinqDsGridViewAllRequests" OnItemCreated="RadGrid_ItemCreated" On>
<GroupingSettings CaseSensitive="false"></GroupingSettings>
<MasterTableView DataSourceID="LinqDsGridViewAllRequests" DataKeyNames="RequestName" AutoGenerateColumns="false" PagerStyle-ShowPagerText="false" PagerStyle-Mode="NextPrevAndNumeric" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="Blue" ShowFooter="false">
<Columns>
<telerik:GridTemplateColumn DataField="RequestName" HeaderText="Request Name" UniqueName="RequestName" HeaderStyle-CssClass="ms-vh2"
SortExpression="RequestName">
<ItemTemplate>
<asp:LinkButton ID="lblRequestName" ForeColor="Blue" CommandArgument='<%# Eval("RequestName") %>' CommandName="onclick" OnCommand="RequestNameLinkButton_Click" runat ="server" Text='<%# Eval("RequestName") %>'></asp:LinkButton>
</ItemTemplate>
<HeaderStyle CssClass="ms-vh2" />
<ItemStyle CssClass="talCell4Grid" HorizontalAlign="Left" VerticalAlign="Top" />
</telerik:GridTemplateColumn>
In the code, lblRequestName is the LinkButton that I want to use for downloading excel.
try to use if (!IsPostBack) wrap all code in Page_Load function, it can prevent page reload when you click the button.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//some code about page load
}
}
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 am having trouble when trying to fire a button in a GridView with the parameter CommandName = "x", I try to reach my "If" in the GridView1_RowCommand event but I just cant for some reason, if you could help me I'd be gratefull .
This is my .aspx part
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
EnableModelValidation="True" ForeColor="#333333" GridLines="None"
Height="193px" Width="968px" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SOURCE1" onrowcommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button Text = "Seleccionar" runat="server" CommandName="X" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="IDEmpresa" HeaderText="IDEmpresa"
SortExpression="IDEmpresa" />
</Columns>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
And this is my C# code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "X")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
Label1.Text = "WOW It reached out";
}
}
I followed the instructions of the ASP.net page, and im fairly new to .net (I dont know if the UpdatePanel has something to do with it)
Doing a quick test, the code you have provided works, but I did have to wire up my own datasource.
What you are missing here is that Label1 is outside of your UpdatePanel and will not refresh based on your localized postback within the UpdatePanel.
A word of caution with GridViews and UpdatePanels/Buttons. Make sure you are not manually binding/rebinding during Page_Load, and if you have code to do so, you do so within an if(!IsPostBack) { } statement.
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 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.