my question is simple, how do i create a custom save and update button for records entered in a detail view. I dont want to use the ones given. thanks a lot.
You have a couple of options. One is the OnItemCommand and you'll roll your own commands.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand.aspx
The easier way is to use the OnItemInserted and OnItemUpdating events. Just send the Insert or Update commands instead as appropriate and you can use easier EventArgs
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.iteminserting.aspx
<http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemupdating.aspx
From these pages, basically what you'll do is capture a command from a button in your DetailsView.
Custom "Add" commamnd with ItemCommand
Sub CustomerDetailView_ItemCommand(ByVal sender As Object, ByVal e As DetailsViewCommandEventArgs)
' Use the CommandName property to determine which button
' was clicked.
If e.CommandName = "Add" Then
' Do your work here if Add Contact is clicked
End If
End Sub
Easier built in Insert command with ItemInserting
Sub CustomerDetailView_ItemInserting((ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
' Access the actual rows with
Some variable1 = e.Values("CustomerID")
Some variable2 = e.Values("CompanyName")
Some variable3 = e.Values("City")
' Do something with them
End Sub
Code front
<asp:DetailsView ID="CustomerDetailView"
DataSourceID="DetailsViewSource"
AutoGenerateRows="false"
DataKeyNames="CustomerID"
AllowPaging="true"
OnItemCommand="CustomerDetailView_ItemCommand"
OnItemInserting="CustomerDetailView_ItemInserting"
OnItemUpdating="CustomerDetailView_ItemUpdating"
runat="server">
<FieldHeaderStyle BackColor="Navy" ForeColor="White" />
<Fields>
<asp:BoundField DataField="CustomerID" HeaderText="Store ID" />
<asp:BoundField DataField="CompanyName" HeaderText="Store Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:TemplateField HeaderText="Name">
<InsertItemTemplate>
<asp:Button ID="btAddContact" runat="server" Text="Add Contact" CommandName="Add" />
Or
<asp:Button ID="btAddContact" runat="server" Text="Add Contact" CommandName="Insert" />
</InsertItemTemplate>
<EditItemTemplate>
<asp:Button ID="btAddContact" runat="server" Text="Save Contact" CommandName="Update" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
You can use any button that implements the IButtonControl interface. The key is to set the CommandName correctly. Hopefully this will give you the freedom to style your button the way you need to.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx
You can find a list of the relevant command names here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand.aspx
<asp:ImageButton runat="server" ... CommandName="Save" />
<asp:LinkButton runat="server" ... CommandName="Update" />
Related
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
I created a simple Grid view with Multiple columns and add a button so when clicked pass the values of the row to a different webform but button on click not firing. I use the example code from the link
: http://msdn.microsoft.com/en-us/library/bb907626(v=vs.100).aspx
And this is my code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1"
PageSize="100" AutoGenerateColumns="False">
<Columns>
<%--<asp:HyperLinkField HeaderText="Edit" NavigateUrl="FormReport2.aspx"
Text="Edit" />--%>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="fldEmployeeID" HeaderText="EmployeeID"
SortExpression="fldEmployeeID" />
<asp:BoundField DataField="fldAbsentDate" HeaderText="AbsentDate"
SortExpression="fldAbsentDate" />
<asp:BoundField DataField="fldAbsentCode" HeaderText="AbsentCode"
SortExpression="fldAbsentCode" />
<asp:BoundField DataField="fldRuleViolationWarningType"
HeaderText="Rule Violation Warning Type"
SortExpression="fldRuleViolationWarningType" />
<asp:BoundField DataField="fldRuleViolationIssueDate"
HeaderText="Rule Violation Issue Date"
SortExpression="fldRuleViolationIssueDate" />
<asp:BoundField DataField="fldLOAEndDate" HeaderText="LOA End Date"
SortExpression="fldLOAEndDate" />
</Columns>
</asp:GridView>
code behind
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "AddToCart") Then
' Retrieve the row index stored in the CommandArgument property.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button
' from the Rows collection.
Dim row As GridViewRow = GridView1.Rows(index)
' Add code here to add the item to the shopping cart.
End If
End Sub
Any help would be very appreciated. And also what would be the best way to pass the values from the row to a different web form from the code behind?
You forgot to sing up for the event. See the last line here:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1"
PageSize="100" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand">
I am new to Vb.net.
I am using gridview and binding it a resultset from database. I am trying to use asp button and associate a codebehind function with it.
But since the rows are generating dynamically, ids of button are incrementing accordingly.
Here is the code -
'<asp:GridView ID="grdProjects" runat="server" CssClass="q_acontent" Width="990px"
AutoGenerateColumns="false" CellPadding="2" ClientIDMode="static" ViewStateMode="Enabled">
<asp:TemplateField HeaderText="Document Name">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" Text='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem, "Data_text")))%>'/>
</ItemTemplate>
<ItemStyle Width="5%" HorizontalAlign="Center" CssClass="conatact_phone breakword" />
</asp:TemplateField>
</asp:GridView>'
Please help me how to associate codebehind function and pass its corresponding button text to it.
Use the OnCommand event of button controls and Set the CommandArgument property of the button to your DataItem value as:
<ItemTemplate>
<asp:Button ID="Button3" runat="server"
Text='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem,"Data_text")))%>'
OnCommand="Button3_Command"
CommandArgument='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem,
"Data_text")))%>'
/>
</ItemTemplate>
OnCommand event in your code behind file:
Public Sub Button3_Command(sender As Object, e As CommandEventArgs)
Dim _dataText As String = e.CommandArgument.ToString()
End Sub
Currently I am making a webapp in asp.net. Normally I use a sqlDataSource and enable paging and deleting etc, but I am not able to do that in this case.
I made a GridView and it shows the data perfectly, how ever my RowDelete event doesn't fire
Code GridView: ( only relevant code )
<asp:GridView ID="GridView1"
runat="server"
onpageindexchanging="GridView1_PageIndexChanging"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField
HeaderStyle-CssClass="head"
ItemStyle-CssClass="items"
HeaderText="Klant">
<ControlStyle ForeColor="#333333" />
<HeaderStyle CssClass="head"></HeaderStyle>
<ItemStyle CssClass="items" Font-Bold="False" ForeColor="#494C50" ></ItemStyle>
<ItemTemplate>
<asp:LinkButton ID="klant" runat="server"
Text='<%#Eval("Bedrijf") %>'
PostBackUrl='<%# "klant_wijzigen.aspx?Klant_ID="+Eval("Klant_ID").ToString()%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderStyle-CssClass="head"
ItemStyle-CssClass="items"
HeaderText="Categorie">
<ItemTemplate>
<asp:LinkButton ID="categorie" runat="server"
Text='<%#Eval("Categorie") %>'
PostBackUrl='<%# "cat_wijzigen.aspx?Cat_ID="+Eval("Cat_ID").ToString()%>'>
</asp:LinkButton>
</ItemTemplate>
<ControlStyle ForeColor="#333333" />
<HeaderStyle CssClass="head"></HeaderStyle>
<ItemStyle CssClass="items" Font-Bold="false" ></ItemStyle>
</asp:TemplateField>
<asp:BoundField
DataField="Website"
headertext="Website"
HeaderStyle-CssClass="head"
ItemStyle-CssClass="items">
<HeaderStyle CssClass="head"></HeaderStyle>
<ItemStyle CssClass="items"></ItemStyle>
</asp:BoundField>
<asp:BoundField
HeaderStyle-CssClass="head"
DataField="Titel"
headertext="Titel"
ItemStyle-CssClass="items"
>
<HeaderStyle CssClass="head"></HeaderStyle>
<ItemStyle CssClass="items"></ItemStyle>
</asp:BoundField>
<asp:TemplateField
HeaderStyle-CssClass="head2"
ItemStyle-CssClass="items2">
<ItemTemplate>
<asp:Button ID="LinkButton1"
runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete"
CssClass="verwijder"
OnClientClick="return confirm
('Weet je zeker dat je het project wilt verwijderen?')">
</asp:Button>
<asp:Button ID="project" runat="server"
Text="Details"
CssClass="details"
PostBackUrl='<%#"Details.aspx?ID="+Eval("ID").ToString()%>'
/>
</ItemTemplate>
<HeaderStyle CssClass="head2"></HeaderStyle>
<ItemStyle CssClass="items2"></ItemStyle>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="head"></HeaderStyle>
<AlternatingRowStyle BackColor="#e1e3e9" />
</asp:GridView>
As you can see they are all bounded. The delete button is a LinkButtton
And this is my script:
Protected Sub Gridview1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
GridView1.DeleteRow(e.RowIndex)
GridView1.DataBind()
End Sub
I bind my GridView like this: I have made 2 seperate SqlDataSources with 2 different Select Queries. Based on on the SelectedValue of my dropdownlist selects the SqlDataSource:
Code
Public Sub Wop() 'Here I set the IF statement.
If DropDownList1.SelectedValue = 0 Then
GridView1.DataSource = SqlDataSource4()
Else
GridView1.DataSource = SqlDataSource2()
End If
GridView1.DataBind()
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Wop() `Here is were I call the IF statement.
End Sub
Any clue would be appreciated.
NOTE
I must say I am not writing in C#!
You look like you're calling DeleteRow from within your RowDeleting event. That doesn't make any sense since I would imagine that RowDeleting is not raised until DeleteRow is called.
Edit:
From MSDN:
Use the DeleteRow method to programmatically delete the record at the
specified index from the data source. This method is commonly used
when you need to delete a record from outside of the GridView control,
such as from a different control on the page. Calling this method also
raises the RowDeleted and RowDeleting events.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.deleterow.aspx
We have a GridView that contains 2 "Select" buttons for each row that is displayed in the GridView.
We would like to know if there is a way to find out which of the 2 buttons was clicked on by using the SelectedIndexChanged handler.
This coding shows the buttons we have:
<asp:UpdatePanel
ID="UpdatePanelParentsSummary"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<p>Parent Search:
<asp:TextBox
ID="TextBoxSearch"
runat="server"
Width="207px"
Text="ALL"> </asp:TextBox>
<asp:Button
ID="ButtonSearch"
runat="server"
Text="Search" />
<asp:Button
ID="ButtonSearchAll"
runat="server"
Text="Show ALL Parents" />
<br />
</p>
<asp:GridView
ID="GridViewParentsSummary"
runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID"
>
<Columns>
<asp:BoundField
DataField="ID"
HeaderText="ID"
SortExpression="ID" InsertVisible="False" ReadOnly="True" Visible="False" />
<asp:BoundField
DataField="FatherName"
HeaderText="FatherName"
SortExpression="FatherName" />
<asp:BoundField DataField="MotherName" HeaderText="MotherName"
SortExpression="MotherName" />
<asp:ButtonField
ButtonType="Button"
CommandName="Select"
Text="Select Details" />
<asp:ButtonField
ButtonType="Button"
CommandName="Select"
Text="New Person To Release Child" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
This is the code in the SelectedIndexChanged handler:
Protected Sub GridViewParentsSummary_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewParentsSummary.SelectedIndexChanged
IntParentsID = GridViewParentsSummary.DataKeys(GridViewParentsSummary.SelectedIndex).Value
Response.Redirect("AuthorizationForChildReleaseDetails.aspx")
End Sub
Ah, there is a simple solution to this problem. Your sender is the button clicked. Just try something like:
ButtonField buttonClicked = sender as ButtonField;
if (buttonClicked != null) {
String commandName = buttonClicked.CommandName;
if (commandName.equals("Command1") {
... do something awesome ...
} else if (commandName.equals("Command2")) {
... do something less awesome ...
}
}
I don't think you can differentiate between the source at the GridView event level because it is an event raised by the GridView at that point which is masking the lower level event. You can, however, implement a row level handler to identify which button was used to raise the event and set it up somewhere for use in the gridview event.
protected void GridView_RowCommand(object sender, CommandEventArgs e)
{
e.CommandArgument ....contains the argument name
....
}