I have a gridview in which when I click edit, update, and cancel button comes. I have a variable named status. If status=false then update should change to insert and if status=true then update should be update itself. What code do I have to write in rowcammand for this?
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" Text="Edit">
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel" Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
You could check for the id in the RowUpdating event and take it from there. You can also offload the decision to a stored proc in the database.
GridView is not designed for insert operations. You should use FormView or DetailView for the insert purpose.
Thought you can check if the record exists in GridView_RowCommand event, you need to filter the command of your interest using condiotnal match and write the code there.
for doing inserts with GridView this might help.
Link
Related
I have the following button on GridView and I want to display a message box to the user for confirming whether he is sure or not for buying the item.(I'm familiar with OnClientClick= return confirm('xxxxx'); ) but on Grid View it is different I can't use this method though.
<asp:ButtonField CommandName="buynow" ImageUrl="~/images/buy.png" HeaderText="BUY" ButtonType="Image"/>
Use itemtemplate field in column node in gridview markup
eg.
<asp:TemplateField HeaderText="Save">
<ItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return confirm('Do you want to save?')" />
</ItemTemplate>
</asp:TemplateField>
Aspx, Entity, on my gridView I made a footer for a new button. It shows when there are items, but not when data is empty. gridView's ShowFooter = True. Not sure why it won't show when empty, is there another property I maybe missing? I know a workaround could be place a dummy row in the database, but think there should be a better way to start off at 0. If you have any more questions, please ask.
<asp:TemplateField HeaderText="New" >
<FooterTemplate >
<asp:Button ID="btnNew" CssClass="DDButton" runat="server" Text="New" CommandName="New" OnClick="new_item" />
</FooterTemplate>
</asp:TemplateField>
The footer-template is not shown when no data is present, only the EmptyDataTemplate.
<EmptyDataTemplate>
<asp:Button ID="btnNew" runat="server"
Text="New" CommandName="New"
CssClass="DDButton"
OnClick="new_item" />
</EmptyDataTemplate>
.Net 4.0 added the ShowHeaderWhenEmpty property on the GridView but unfortunately no ShowFooterWhenEmpty. However there is still the EmptyDataTemplate:
<asp:GridView runat="server" ID="myGridView">
<EmptyDataTemplate>
<asp:Button ID="btnNew" CssClass="DDButton" runat="server" Text="New" CommandName="New" OnClick="new_item" />
</EmptyDataTemplate>
</asp:GridView>
As this will not be in a GridViewRow I don't think your RowCommand event will fire but you seem to be handling the click of the button in a separate method anyway.
Reference for GridView.EmptyDataTemplate: http://msdn.microsoft.com/en-gb/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
I'm a newbie when it comes to asp.net, so grateful for any help.
I have a simple data bound drop down list, with a details view control. I can select a value from the list, hit update and the correct value gets written to the database. Problem is, the control the automatically "resets" to display the 1st value in the list. This would confuse the user and make them think they'd selected the 1st value prior to the update.
The only code-behind code in relation to this drop down list can be found in the ItemUpdating method of the details view control, as follows:
DropDownList ddlLoc = (DropDownList)dvYourProfile.FindControl("ddlLocation");
e.NewValues["PreferredLocation"] = ddlLoc.SelectedValue;
And here's the code form the aspx page
<asp:TemplateField HeaderText="Preferred Location"
SortExpression="PreferredLocation">
<ItemTemplate>
<asp:DropDownList Text='<%# Bind("PreferredLocation") %>' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:SqlDataSource ID="dsStaticDate" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT * FROM StaticData" />
You need to bind form the code behind. The out-of-the-box behavior of binding on the aspx page with the SqlDataSource does not allow you to stop the rebinding of the DropDownList after an event occurs.
I have a basic ASP.NET Web Forms application.
I give the user the possibility to create the records by using a Web Form and to update them by using a DetailsView.
The web form stores the empty field on the TextBoxes as *empty strin*g in the DB correctly.
The DetailsView instead stores them as null fields in the DB.
Moreover in the DetailsView_ItemUpdated event if I check the e.newvalues and e.oldvalues arguments the corresponding fields are null as well (throwing an exception) that means the data are already sent as null to the server upon submit.
In the DetailsView I use templates and I set all Update parameters in the SQLDataSource as ConvertEmptyStringToNull="false".
SQLDataSource parameter:
<asp:Parameter Name="SHELF" Type="String" ConvertEmptyStringToNull="false"/>
DetailsView
<asp:DetailsView runat="server"
ID="GvProductDetail" AutoGenerateRows="False" DataKeyNames="rowid"
DataSourceID="ProductDetailData"
OnItemUpdated="gvProductDetail_ItemUpdated">
<asp:TemplateField>
<HeaderTemplate>Stock Shelf </HeaderTemplate>
<ItemTemplate>
<%# Eval("Shelf") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShelf" Text='<%# Bind("Shelf") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Is that possible that Bind converts the empty values to null? I thought it was enough to set the UpdateParameters rule not to convert empty string to null but apprently there is some other surprise. anybody might help?
I solved it by placing ConvertEmptyStringToNull="false" to every <asp:TemplateField> containing <asp:TextBox>:
<asp:TemplateField ConvertEmptyStringToNull="false">
<HeaderTemplate>Stock Shelf </HeaderTemplate>
<ItemTemplate> <%# Eval("Shelf") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShelf" Text='<%# Bind("Shelf") %>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
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();