click event to update the gridview in ASP - asp.net

It convenient to do the query by :
<asp: AccessDataSource >
I wish to update the query string and update the result by a click event.
<asp:Button id="Button1"
Text="To Inquiry"
OnClick="doInquiry"
runat="server"/>
How I can achieve to update the query string and do the query in in the "doInquiry" function; BUT, still using the following useful code.
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Northwind.MDB" SelectCommand="SELECT * FROM [Categories]"></asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="AccessDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>

Whenever you generate the HTML via the markup and the behavior is different from what you expect, check the generated HTML in your browser. You intended to set the onclick attribute of an HTML tag, however, OnClick is a property pointing to the server-side method, which manages the server-side click event. You intended to use the OnClientClick property.

Related

DetailsView in Modal - Modal won't open, conflict with data-toggle

I have a GridView and DetailsView on a page - both databound.
I want to select a databound row and open the modal with the populated DetailsView inside.
I have LinkButtons within a TemplateField for my select button with the sample bootstrap modal data-toggle and data-target fields.
My problem is with showing the DetailsView in the modal - upon clicking the SELECT button in the GridView - the modal shows, but the data is stays the same regardless of which row I select.
DetailsView Code
<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="DetailsViewComputer" Height="50px" Width="125px">
</asp:DetailsView>
<asp:SqlDataSource ID="DetailsViewComputer" runat="server" ConnectionString="<%$ ConnectionStrings:ITManagementConnectionString %>" ProviderName="<%$ ConnectionStrings:ITManagementConnectionString.ProviderName %>" SelectCommand="SELECT idComputers, idStatus, Hostname, idUser, AssetNumber, IPAddress, MACAddress, idoffice, idManufacturer, idModel, idProcessor, idRAM, idRAMType, idGraphicsCard, idHDD, SerialNumber, DateCreated, DateLastModified FROM computers WHERE (idComputers = #Param1)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Param1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
GridView Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="idComputers" DataSourceID="GridViewComputer">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select" data-toggle="modal" data-target="#myModal" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="idComputers" HeaderText="idComputers" InsertVisible="False" ReadOnly="True" SortExpression="idComputers" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:BoundField DataField="Hostname" HeaderText="Hostname" SortExpression="Hostname" />
<asp:BoundField DataField="forename" HeaderText="forename" SortExpression="forename" />
<asp:BoundField DataField="surname" HeaderText="surname" SortExpression="surname" />
<asp:BoundField DataField="officename" HeaderText="officename" SortExpression="officename" />
<asp:BoundField DataField="IPAddress" HeaderText="IPAddress" SortExpression="IPAddress" />
<asp:BoundField DataField="ManufacturerName" HeaderText="ManufacturerName" SortExpression="ManufacturerName" />
<asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
<asp:BoundField DataField="HDDSize" HeaderText="HDDSize" SortExpression="HDDSize" />
<asp:BoundField DataField="RAMSize" HeaderText="RAMSize" SortExpression="RAMSize" />
</Columns>
</asp:GridView>
Through the process of elimination, I think the issue has something to do with my LinkButton - specifically with data-toggle - see below:
LinkButton1
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select" data-toggle="modal" data-target="#myModal" Text="Select"></asp:LinkButton>
Whenever I remove the data-toggle element and the DetailsView code from the Modal - the select works and the DetailsView displays the data. Whenever the data-toggle element is included and the DetailsView code is in the Modal - the Modal Shows without but the DetailsView data doesn't change.
Am I missing something?
I have searched and tried the solution in the below page, but either I'm an idiot or it didn't work (most likely the former):
Open popup with linkbutton
I would appreciate any help with this whatsoever! I have spent all day scratching my head because of this.
Thanks in Advance,
Chris
I found a solution to the issue by opening the Modal using JS - See below:
http://getbootstrap.com/javascript/#js-programmatic-api
I removed
data-toggle="modal" data-target="#myModal"
from LinkButton and instead added onClick="LinkButton1_Click" - see below:
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" OnClick="LinkButton1_Click" CommandName="Select" Text="Select"></asp:LinkButton>
I then referenced it in the VB.NET code behind:
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "myModal", "$('#myModal').modal()", True)
End Sub

C# How do I create a Hyperlink OnClick event on GridView?

I'm having trouble creating the GridView I want.
I would like the user to get inside the website and see the GridView which is attached to a DB.
Columns are: ID, InsertionTime, Filepath, ProccessedByUser
Now I want the user to click the filepath he/she would like to process. When he/she clicks the filepath, I want their username (logged in with built-in asp website authentication) to be updated (added) into DB.
My markup is standard and I haven't got to manage with code behind.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="starttime" HeaderText="starttime"
SortExpression="starttime" />
<asp:HyperLinkField DataNavigateUrlFields="path" DataTextField="path"
HeaderText="path" />
<asp:BoundField DataField="user" HeaderText="user" SortExpression="user" />
</Columns>
</asp:GridView>
I tried using HyperlinkField but it doesn't seem to support onlick events.
Any suggestions?
Thanks.
I assume you are looking for the LinkButton control which has an OnClick event.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="starttime" HeaderText="starttime"
SortExpression="starttime" />
<asp:TemplateField HeaderText="Path" SortExpression="Filepath">
<ItemTemplate>
<asp:LinkButton ID="LbPath" runat="server"
Text='<%# Eval("Filepath") %>'
CommandName="PathUpdate"
CommandArgument='<%#Bind("path") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="user" HeaderText="user" SortExpression="user" />
</Columns>
</asp:GridView>
Now you can handle the LinkButton's click event or the GridView's RowCommand event.
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "PathUpdate")
{
string path= e.CommandArgument.ToString();
// do you what you need to do
}
}
Note that i've used a TemplateField which is the most dynamic column type in a GridView since you can add anything you want, even nested GridViews or UserControls.
You could use a ButtonField, and then handle OnRowCommand of the gridview. Example here:
http://msdn.microsoft.com/SV-SE/library/system.web.ui.webcontrols.buttonfieldbase.buttontype.aspx
You can set the ButtonType attribute of ButtonField to display the button as a Link.

How to add a 'Record Deleted' message in this code?

I want to add a Record Delete Message when the Delete button is selected. The message would appear for 5 seconds, then disappear. Here's the code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" DataKeyNames="ScheduleId">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="SDate" HeaderText="SDate" SortExpression="SDate" />
<asp:BoundField DataField="STime" HeaderText="STime" SortExpression="STime" />
<asp:BoundField DataField="SNoPlaces" HeaderText="SNoPlaces"
SortExpression="SNoPlaces" />
<asp:BoundField DataField="ScheduleId" HeaderText="ScheduleId"
InsertVisible="False" ReadOnly="True" SortExpression="ScheduleId" Visible="false" />
<asp:BoundField DataField="STourId" HeaderText="STourId" SortExpression="STourId" Visible="false" />
<asp:BoundField DataField="TName" HeaderText="Tour Name" SortExpression="TName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>"
DeleteCommand="DELETE FROM Schedule WHERE (ScheduleId = #ScheduleId)"
SelectCommand="SELECT SDate, STime, SNoPlaces, ScheduleId, STourId, TName FROM Schedule INNER JOIN Tour ON Schedule.STourId = Tour.TourId">
<DeleteParameters>
<asp:Parameter Name="ScheduleId" />
</DeleteParameters>
</asp:SqlDataSource>
Regards
Tea
Use this gridview event:
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
LabelMsg.Text = "Record Deleted";
}
I would use jQuery to always Fadeout the LabelMsg element on $(document).ready(). So no matter what you put in it, from the server side, it will fade out in the time you decide.
I don't think you can accomplish this naively as part of a GridView but you can wire up the RowDeleted event to inject some JavaScript using the ClientScriptManager.RegisterStartupScript method.
I would suggest using toastr to display your actual message and it can be added to your project with its associated nuget package.
RowDeleted
RegisterStartupScript
toastr

How to validate controls in each column of a gridview

I would like to validate columns in an asp:Gridview but am unsure how to do so as all the tutorials I have found show a previous version on Microsoft Visual Studio (I am using 2010).
How do I validate each column of the Gridview?
Thanks
This is the code of my Gridview generated by visual studio.
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="registrationNo" DataSourceID="SqlDataSource3"
onselectedindexchanged="GridView2_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="fullName" HeaderText="fullName"
SortExpression="fullName" />
<asp:BoundField DataField="address" HeaderText="address"
SortExpression="address" />
<asp:BoundField DataField="work" HeaderText="work"
SortExpression="work" />
<asp:BoundField DataField="home" HeaderText="home" SortExpression="home" />
<asp:BoundField DataField="mobile" HeaderText="mobile"
SortExpression="mobile" />
<asp:BoundField DataField="registrationNo" HeaderText="registrationNo"
InsertVisible="False" ReadOnly="True" SortExpression="registrationNo" />
<asp:ButtonField ButtonType="Button" CommandName="Edit" HeaderText="Edit"
ShowHeader="True" Text="Edit" />
<asp:ButtonField ButtonType="Button" CommandName="Update" HeaderText="Update"
ShowHeader="True" Text="Update" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete"
ShowHeader="True" Text="Delete" />
</Columns>
<HeaderStyle BorderColor="#33CC33" />
<RowStyle BorderStyle="Double" Font-Names="Monotype Corsiva" />
</asp:GridView>
//native asp.net clientside validation (ATLAS Ajax)
Instead of using BoundFields you should use TemplateField. This will allow you to add Validation controls in the field i.e.
Tutorial from 2007 which is still pertinent to this question
//serverside validation
In order to validate the values on the server side you have a couple of options:
a) add a CustomValidator pointing to codebehind server validation method i.e. msdn doc
b) on the gridviews RowUpdating method you could validate manually (this would also be for batch updates)

Adding a validator to the gridview textbox, created in edit-mode of a bound field

take a look at this sample code: (question bellow)
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource2"
AutoGenerateColumns="False" onrowupdated="GridView1_RowUpdated"
DataKeyNames="Product_Id">
<Columns>
<asp:ImageField DataImageUrlField="Image_Name" HeaderText="Image_Name"
ReadOnly="True" >
<ItemStyle Width="50px" Height="50px" Wrap="true"/>
</asp:ImageField>
<asp:BoundField DataField="Product_Id" HeaderText="Product_Id"
InsertVisible="False" ReadOnly="True" SortExpression="Product_Id">
</asp:BoundField>
<asp:BoundField DataField="Product_Name" HeaderText="Product_Name"
SortExpression="Product_Name" />
<asp:BoundField DataField="Category_Name" HeaderText="Category_Name"
SortExpression="Category_Name" ReadOnly="true" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Size" HeaderText="Size"
SortExpression="Size" />
<asp:BoundField DataField="Price" HeaderText="Price"
SortExpression="Price" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Assume I initialize an SqlDataSource, add a parameter and so on.
The thing is, that when a user clicks edit we get a textbox to edit the colnumn value.
I want to validate the data enter by the user before the update is performed and the new
data is propagated back to the server.How?
10x a lot!
You need to convert the BoundField into a TemplateField. Then you can add a validator to the actual TextBox control.
Option 1:
But from the UNKNOWN answer, the Microsoft recommends the same.. as he told.
ref: http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic2
Option 2:
But, we can do.
You need to add the validation either the javascript validation or server side validation
control, when the GridView's DataBound event is happening at runtime on the particular
TableCell of the Gridview rows.
Hence, when you click the update button that custom generated javascript or the validation
control will check for the validation on editing the values.
This process is more harder than the converting boundfield to templatefield
refer: http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx
Option 3:
And you can go for server side validation on the values instead of client side validation:
refer: http://msdn.microsoft.com/en-us/library/bb332383.aspx

Resources