I have an LinkButton column in my GridView:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
let's assume that I need to bound to that GridView a list of some items, where some of them shoud have visible that LinkButton, some not. So that is the question: how - while bounding/after bound - can I realize that scenario, I mean show LinkButtons (with different CommandArgument) where there are needed ?
You have 3 options:
Handle the RowDataBound event: find the button in the row and set the visible property
Bind a property/column in the datasource: <asp:LinkButton Visible='<%# Bind("Editable") %>'
create a method returning a boolean in the page and use it: <asp:LinkButton Visible='<%# IsButtonVisible(DataBinder.Eval(Container.DataItem, "ID")) %>'
Related
I have a gridview with a dropdownlist for the products description in the footer template.
There is no way to create a SelectedIndexChanged in the IDE and writing it out manually produces an error? How to create code to handle the Selection change? I need to populate the product ids when the product description is selected.
"EDITED"
I tried using the gridview rowediting event assuming that if a row item was changed (ie, a new selection in the dropdown, it would fire, but it doesn't) It appears a gridview event has to be fired when that dropdown list changes, that's where i need the code to go. Any ides on what event?
Here is what the template field markup is:
<asp:TemplateField HeaderText="description" SortExpression="description">
<FooterTemplate>
<asp:DropDownList ID="ddlProductDesc" runat="server" DataSourceID="edsProductDesc" DataTextField="description"
OnSelectedIndexChanged="ddlProductDesc_SelectedIndexChanged">
</asp:DropDownList>
<%--<asp:TextBox ID="tbInsertdescriptiton" Width="350" runat="server"></asp:TextBox>--%>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblProdDesc" runat="server" Text='<%# Bind("description")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Take a look at this:
http://www.c-sharpcorner.com/blogs/7228/asp-net-gridview-dropdown-with-related-records-in-textbox.aspx
I am using a multi page gridview to display bunch of data. Here is the code of the gridview.
<asp:GridView ID="unverifiedlist" runat="server" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" OnRowDataBound="unverifiedlist_RowDataBound" style="font-size:12px" >
<Columns><asp:BoundField HeaderText="Surname" DataField="Surname" ReadOnly="true"/></Columns>
<Columns><asp:BoundField HeaderText="Firstname" DataField="Givenname" ReadOnly="true"/></Columns>
......
<Columns><asp:TemplateField HeaderText="Options" >
<ItemTemplate>
<asp:DropDownList ID="options" runat="server" AutoPostBack="true" OnSelectedIndexChanged="options_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1">Verified</asp:ListItem>
<asp:ListItem Value="2">Rejected</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Reason">
<ItemTemplate>
<asp:TextBox ID="reason" runat="server" OnTextChanged ="reason_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Visible="false"/>
</asp:GridView>
<asp:DropDownList ID="PageSelect" AutoPostBack="true" runat="server" OnSelectedIndexChanged="PageSelect_SelectedIndexChanged"></asp:DropDownList>
there is a column called "option" which contains a dropdownlist, and it will postback everytime when the selected index was changed to save the edited row. I am trying to avoid this frequent postback. So i think if I can save the whole page before user turn to the next page.
The problem is the "PageSelect" dropdownlist's postback will trigger the page reload first, then onselectedindexchanged event, at this point, the gridview already turned to the next page. So can anyone give me some advice?
You can set EnableSortingAndPagingCallBacks property of the grid view to true , but that wont work if you are using template fields in your gridview, for maintaining the state of the page, you will have to keep the value of current page in session and on page load you can check the session..
I used the gridview itself's paging navigation control instead of my dropdownlist at last. Then I am able to use the pageindexchanging event to save the current page into a session before it goes to the next page
I have problem with DropDownList template in Grid View, after I go in edit mode drop down have to be selected by current item instead of default value.
<asp:TemplateField HeaderText="Tour Type">
<EditItemTemplate>
<asp:DropDownList AppendDataBoundItems="true" DataSourceID="dropDownListSqlDataSource" runat="server" DataValueField="idTypetour" DataTextField="title"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<%#Eval("typeTitle")%>
</ItemTemplate>
</asp:TemplateField>
I tried to use SelectedValue="<%#Eval("typeTitle")%>", but has no results.
Try to use Bind instead of Eval:
SelectedValue='<%# Bind("idTypetour") %>'
The DataValueField is "idTypetour" and the DataTextField is "title", you have tried to use Eval("typeTitle"). What column is typeTitle? I assume that this must be idTypetour because you want to set the SelectedValue, what normally is the ID.
Here are informatons on the differences:
http://msdn.microsoft.com/en-us/library/ms178366.aspx
I have a DetailsView control with a template field as follows:
<asp:TemplateField SortExpression="Title">
<ItemTemplate>
<asp:TextBox ID="txtMessageTitle" Text='<%# Bind("Title") %>' runat="server">
</asp:TextBox>
<asp:Button ID="btnUpdateTitle" runat="server" Text="Update"
CommandName="UpdateTitle" CommandArgument='<%# Bind("MessageID") %>' oncommand="btnUpdateCommand"/>
</ItemTemplate>
</asp:TemplateField>
The Details View is wrapped inside an UpdatePanel.
When the user clicks on btnUpdateButton I'd like to be able to retrieve the textbox (txtMessageTitle) value in code behind and by using CommandArgument of the button, update the appropriate MessageTitle in database. How can I retrieve the textbox value inside the DetailsView control from the Command event of my button?
Thanks.
use the following:
TextBox txtMessageTitle = detailsViewName.FindControl("txtMessageTitle") as TextBox;
string text = txtMessageTitle.Text;
I have a GridView that is populated from a LinqDataSource. When I update a row, the RowCommand fires and the change is persisted to the database, but the Grid does not refresh. I have it in an UpdatePanel and explicitely call Update() in the RowCommand handler, but there is no postback and the page just sits there in Edit mode. Once I click cancel, it will return to view-only and the grid shows the new value.
My suspicion is that something in the wiring of the GridView regarding the data source is wrong. No exception bubbles up, though. A stripped-down copy of the markup is below. Any ideas?
<asp:UpdatePanel ID="uPanel" runat="server" UpdateMode="Conditional"
EnableViewState="true" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:LinqDataSource ID="YieldDataSource" runat="server"
ContextTypeName="myhDataContext" TableName="vw_drug_yields"
OnSelecting="YieldDataSource_Selecting" EnableUpdate="true" />
<asp:GridView ID="YieldGridView" runat="server" Width="900px"
OnRowDataBound="editGrid_RowDataBound"
DataSourceID="YieldDataSource" EnableViewState="true"
OnRowCommand="YieldGridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Net Fill" ItemStyle-HorizontalAlign="Center">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "net_fill") %>
</ItemTemplate>
<EditItemTemplate><asp:TextBox ID="tbNetFill" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "net_fill") %>' >
</asp:TextBox></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" ItemStyle-Width="40px">
<ItemTemplate>
<asp:ImageButton CommandName="Edit" ID="btnEdit" SkinID="btnEdit"
runat="server" ToolTip="Edit" CausesValidation="false"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton CommandName="Update" ID="btnSubmit" SkinID="btnSubmit"
runat="server" ToolTip="Save" CausesValidation="true"
CommandArgument="<%# ((GridViewRow) Container).DataItemIndex %>" />
<asp:ImageButton CommandName="Cancel" ID="btnCancel" SkinID="btnCancel"
runat="server" ToolTip="Cancel" CausesValidation="false"/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></ContentTemplate></asp:UpdatePanel>
The handler:
protected void YieldGridView_RowCommand(Object sender,
GridViewCommandEventArgs e) {
if (e.CommandName == "Update") {
try {
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gdrow = YieldGridView.Rows[index];
// do some validation and handle update
db.SubmitChanges();
YieldGridView.DataBind();
uPanel.Update();
}
catch (Exception ex) {
ShowError(this, "Error while updating yields", ex, true);
}
}
After removing the UpdatePanel and turning off callbacks for the GridView, I received the following error when clicking the Update button:
Could not find a row that matches the
given keys in the original values
stored in ViewState. Ensure that the
'keys' dictionary contains unique key
values that correspond to a row
returned from the previous Select
operation.
The problem is that there is no single unique key for this data, as it is dynamically assembled from different sources. It should not be needed, but it seems that a GridView with LinqDataSource can't function without it.
In addition, I am populating the grid from a View, which does not have a primary key.
The solution involved 3 changes:
disabling updates for the LinqDataSource
changing the command name from Update to MyUpdate (so that Linq does not try to auto-wire it)
setting the YieldGridView.EditIndex = -1 prior to calling Update on the UpdatePanel
Thanks for your help.
Before you call Update(), call GridView.DataBind() to rebind against the LINQDataSource control.
try this:
YieldGridView.DataBind();
//though this seems useless after call to DataBind, but lets just try :)
YieldGridView.EditIndex = -1;
uPanel.Update();