As the title suggests, I am attempting to set the UpdateMehod of a GridView to false. When the Update buttons is currently clicked, the event fires validation of a separate control, even though the separate control is in a separate validation group. The grid view is shown below:
<asp:GridView ID="BikesItems"
runat="server"
CssClass="table table-hover"
ItemType="WLL.DAL.Bike"
DataKeyNames="BikeID"
SelectMethod="BikesItems_GetData"
UpdateMethod="BikesItems_UpdateItem"
DeleteMethod="BikesItems_DeleteItem"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
AutoGenerateColumns="false"
ValidationGroup="BikeGrid">
<Columns>
<asp:DynamicField DataField="BikeID" />
<asp:DynamicField DataField="BikeName" />
<asp:DynamicField DataField="Description" />
<asp:DynamicField DataField="ImgPath" />
<asp:DynamicField DataField="Price" />
<asp:DynamicField DataField="ManufacturerID" />
</Columns>
</asp:GridView>
How do I set the CausesValidation to false for the update button, bearing in mind it is dynamically generated? Or does a more elegant solution exist? Thanks
If you just wish to disable the validation group in the code behind (server-side), Use Page.GetValidators("ValidationGroup") method. You can pass in the name of the ValidationGroup and the method returns a ValidatorCollection over which you can iterate.
Or ClientSide, you can iterate over the Page_Validators array and check each validator group name that matches the group you're looking for and disable each as needed.
Related
I am working through an ASP.NET C# tutorial. I have a simple Gridview with the auto-created Edit & Delete Commandfields. So I've converted the Delete command field to a Templatefield and now I am trying to access the RowDataBound event of the Datagrid to add some code.
When I view the Gridview properties and click on the Events, I can see the RowDataBound event, but when I double-click on that event nothing happens. How do I create my event code? (in fact, I can't double-click on any of the events - they are all disabled).
Here is the top section of code for my gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="colorID"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" ShowFooter="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<%-- Edit Button --%>
<asp:CommandField ShowEditButton="True" />
<%-- Delete Button --%>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Thanks for your help.
John
When I create a gridview, the turn-on of edits, deletes, and inserts as commandfields works fine. Also if I immediately convert the delete commandfield to a templatefield before I do anything else, then the RowDataBound event (and all of the other events) are active and I can build codebehind content.
So, for now, something in the tutorial I am using is changing the character of the gridview events. Mostly, I was trying to dialog "Are you sure you want to delete Item XYZ?" as a validating Eval of the row I am deleting. But for now, with too many other things to learn, I'll just live with the stock OnClientClick="return confirm('Are you sure?'); and move on to something else that's more important.
The Alternative BackColor property works if there is data in datagridview . I want to show Alternative BackColorif it is empty.
When declaring your GridView, you can configure options in the EmptyDataRowStyle and the EmptyDataRowStyle elements. So I suppose you can assign the same value to the BackColor in both EmptyDataRowStyle and AlternatingRowStyle elements like in the sample below.
<asp:GridView runat="server" ID="myGridView">
<EmptyDataTemplate>No data</EmptyDataTemplate>
<EmptyDataRowStyle BackColor="Red" />
<AlternatingRowStyle BackColor="Red" />
</asp:GridView>
I have a simple grid on my ASPX page and I am binding it with data coming from a select query on button click event. I am not sure how to bind columns of this table with my grid as currently I am getting 8 columns, 4 with the header given in aspx page and 4 with headers of table columns. Below is my button click event code.
protected void btnSearch_Click(object sender, EventArgs e)
{
MyBookListCont myBookListCont = new MyBookListCont();
gdvMyBooks.DataSource = myBookListCont.SearchBookDetailsCont();
gdvMyBooks.DataBind();
}
And below is aspx code of gridview.
<asp:GridView ID="gdvMyBooks" runat="server">
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
</Columns>
</asp:GridView>
Looks like a silly question, but help would be much appreciated.
This is because you have explicitly specified 4 columns in this design code :
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
</Columns>
Now here, as you can see, there is a HeaderText property which will override the table column name and display the text that is mentioned in here.
You have a couple of options here that you can try :
First, if you want all columns like this with custom header text, you can define all the other columns with these 4 in the same manner as well. That will display all the data and column headers as per the given format.Something like this :
<asp:GridView ID="gdvMyBooks" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
//other columns using same syntax as above.
</Columns>
</asp:GridView>
Also, if you dont want to bind all columns coming in your query, you can use this attibute in your gridview,
AutoGenerateColumns = False
And just specify the columns that you need like you have currently done.
The other thing is, if you want to straight away bind the result in your query to your GridView, then just remove those 4 BoundField statements and leave as it is. This will bind your table to GridView with the same header names as the column names.
I hope this makes things clear.
You have to set your GridView.AutoGenerateColumns to false :
<asp:GridView ID="gdvMyBooks" runat="server" AutoGenerateColumns="False">
Otherwise, it will bind all the field of your object.
I have this code on a gridview
<asp:GridView ID="ReconcileGridView" runat="server" EmptyDataText="Please Select Transaction Month And Date" AutoGenerateSelectButton="True"
BackColor="White" BorderColor="#4A3C8C" BorderWidth="3px" GridLines="Both" BorderStyle="Solid"
CellPadding="3" AllowPaging="True" OnPageIndexChanging="ReconcileGridView_PageIndexChanging"
OnRowDataBound="ReconcileGridView_RowDataBound" OnSelectedIndexChanged="ReconcileGridView_SelectedIndexChanged">
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
<EmptyDataTemplate>
<center>
No Record Found
</center>
</EmptyDataTemplate>
</asp:GridView>
The Problem is on page Load i want the word Please Select Transaction Month And Date to appear and when a user inputs the a transaction month and date and no data was retrieved
No Record Found should show, how would i do that? on the code above only the word No Record Found shows, i Databind Empty Data on page load to make those messages appear.
please help on making that happen.
You have to place a separate Label control for word Please Select Transaction Month And Date
and on page load, you have to set the label control visible property true
and when user inputs the month and date then you can set the lable control property false.
make sense?
You have to place a separate Label control for "Please Select Transaction Month And Date" and on page load, you have to set the label control visible property true and when user inputs the month and date then you can set the label control property false.
Make sure not to bind your Gridview in the page load.
If you are placing the Gridview in an update panel and binding to null to clear it, then the EmptyDataTemplate will be shown. You most likely want to label to shown and not the EmptyDataTemplate. So you will need to hide the entire GridView (if not displaying headers) or you will need to add a cssClass to hide the EmptyDataTemplate at the appropriate points in the codebehind (alternatively use javascript).
What I want to do is just display a few specific columns from my data source on my gridview, but for some reason I'm seeing first the boundfields that I want, followed by every column in the data source.
<asp:GridView DataSourceId="dsTasks" ID="TasksGridView" runat="server">
<Columns>
<asp:BoundField DataField="field1" HeaderText="field1" />
<asp:BoundField DataField="field2" HeaderText="field2" />
<asp:BoundField DataField="field3" HeaderText="field3" />
<asp:BoundField DataField="field4" HeaderText="field4" />
<asp:BoundField DataField="field5" HeaderText="field5" />
<asp:BoundField DataField="field6" HeaderText="field6" />
<asp:BoundField DataField="field7" HeaderText="field7" />
</Columns>
</asp:GridView>
and here is my code for the datasource, just in case thats where it is
<asp:EntityDataSource ID="dsTasks" runat="server"
ConnectionString="name=Entities" DefaultContainerName="Entities"
EnableFlattening="false" EntitySetName="Tasks" ></asp:EntityDataSource>
Sorry if this is a stupid question...I'm fairly new to .NET in general (try a week)
Also, as a side question, would it be better for me to bind the data this way, or do it in the code-behind on page_load?
Set AutoGenerateColumns to false
<asp:GridView AutoGenerateColumns="false" DataSourceId="dsTasks" ...
Personally, I prefer binding from markup than from code when and where it's possible.
You might have to set autogeneratecolumn to false for the gridview