ASP.NET : Displaying an alert from C# code-behind - c#

I have an asp.net page with a c# code-behind. I am trying to have the code-behind display an 'alert' if the selected-index of a gridview object is changed without selecting 'confirm' or 'cancel'. The code for detecting if confirm or cancel was selected is working, however my message is never displayed. The 'Alert.Show" code was borrowed from: http://archive.devnewz.com/devnewz-3-20061129JavaScriptAlertShowmessagefromASPNETCodebehind.html .
Alert.show works just fine when tested from the page_load(), for example, but not in my selected_index_changed method. Any idea why? Perhaps having to do with how Alert.Show() is implemented?
if (ChangeAttemptedId && !IsSavedId)
{
Alert.Show("Dispatch assignment saved, but you forgot to click Confirm or Cancel!)");
}
ASP.NET CODE:
<asp:Table ID="Table1" runat="server" CssClass="DefaultTable">
<asp:TableRow runat="server">
<asp:TableCell runat="server" Width="50%" VerticalAlign="Top" HorizontalAlign="Left">
<asp:UpdatePanel ID="detailsUP" runat="server" UpdateMode="Always" ChildrenAsTriggers="True">
<ContentTemplate>
<!--
<asp:Label ID="label1" runat="server" Text="Car To Dispatch: " CssClass="DefaultLabel"></asp:Label>
<asp:DropDownList ID="CarsDDL" runat="server" DataSourceID="VehiclesEDS" DataMember="CarNum" DataTextField="CarNum" AppendDataBoundItems="True" Font-Bold="True">
<asp:ListItem Selected="True" Text="-"></asp:ListItem>
</asp:DropDownList>
-->
<asp:DetailsView ID="RideToAssignDV" runat="server" Height="400px"
Width="400px" AutoGenerateRows="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<EditRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="AssignedCar" HeaderText="Car"
SortExpression="AssignedCar" NullDisplayText="---" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" NullDisplayText="---" />
<asp:BoundField DataField="Phone" HeaderText="Phone"
SortExpression="Phone" NullDisplayText="---" />
<asp:BoundField DataField="NumPatrons" HeaderText="Size"
SortExpression="NumPatrons" NullDisplayText="---" />
<asp:BoundField DataField="PickupAddress" HeaderText="Pickup Address"
SortExpression="PickupAddress" NullDisplayText="---" />
<asp:BoundField DataField="DropoffAddress" HeaderText="Drop-Off Address"
SortExpression="DropoffAddress" NullDisplayText="---" />
<asp:BoundField DataField="CreatedBy" HeaderText="Created By"
SortExpression="CreatedBy" NullDisplayText="---" />
<asp:BoundField DataField="TimeOfCall" HeaderText="Call Time"
SortExpression="TimeOfCall" ReadOnly="True" NullDisplayText="---" />
</Fields>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" BorderStyle="Inset" BorderColor="#C6940D" HorizontalAlign="Center" Height="25px" />
<FooterTemplate>
<asp:Button ID="confirmButton" runat="server" Text="Confirm" ForeColor="Green" HorizontalAlign="Center" OnClick="confirmButton_Click"/>
<asp:Button ID="cancelButton" runat="server" Text="Cancel" ForeColor="Red" HorizontalAlign="Center"
OnClick="cancelButton_Click" OnClientClick="displayTopTen();" />
</FooterTemplate>
<HeaderStyle BackColor="#004812" Font-Bold="True" />
<PagerStyle BackColor="#999999" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:TableCell>
<asp:TableCell runat="server" Width="50%">
<asp:UpdatePanel ID="mapUP" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="map_canvas" style="height: 400px; width:400px;"></div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<asp:Label ID="GV_Label1" runat="server" Text="Car To Dispatch: " CssClass="DefaultLabel"></asp:Label>
<asp:UpdatePanel ID="SelectCarUP" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="VehiclesGridView" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="VehiclesEDS" AutoGenerateColumns="False"
onselectedindexchanged="VehiclesGridView_SelectedIndexChanged"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" ShowHeaderWhenEmpty="True" AutoPostBack="True">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="GVSelectButton" runat="server" CausesValidation="False"
CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CarNum" HeaderText="Car" ReadOnly="True"
SortExpression="CarNum" />
<asp:BoundField DataField="CurrPassengers" HeaderText="Passengers"
ReadOnly="True" SortExpression="CurrPassengers" />
<asp:BoundField DataField="MaxPassengers" HeaderText="Capacity" ReadOnly="True"
SortExpression="MaxPassengers" />
<asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True"
SortExpression="Status" />
<asp:BoundField DataField="StartAdd" HeaderText="Pick-Up Address"
ReadOnly="True" SortExpression="StartAdd" />
<asp:BoundField DataField="EndAdd" HeaderText="Drop-Off Address"
ReadOnly="True" SortExpression="EndAdd" />
<asp:BoundField DataField="AvgRideTime" HeaderText="Avg. Ride Time"
ReadOnly="True" SortExpression="AvgRideTime" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#004812" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#C6940D" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#C6940D" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#9F770B" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

Description
Assuming i understand your question.
You can use the ScriptManager to show a javascript alert message.
Sample
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
"err_msg",
"alert('Dispatch assignment saved, but you forgot to click Confirm or Cancel!)');",
true);
}
More Information
MSDN - ClientScriptManager.RegisterStartupScript Method

private void MessageBox(string message,string title="title")
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), title, "alert('" + message + "');", true);
}
It can be useful:
http://www.codeproject.com/Questions/311503/How-to-use-javascript-alert-message-in-code-behind

If you want a single piece of client-side JavaScript to run when the page loads, you can register a startup script in your code-behind:
if(!ClientScript. IsStartupScriptRegistered(typeof(Page), "alert"))
string script = "<script>";
script += "alert('";
script += "Dispatch assignment saved, but you forgot to click Confirm or Cancel!";
script += "');";
script += "</script>";
ClientScript.RegisterStartupScript(typeof(Page), "alert", script);
}
ASP.NET will take care of putting the <script> in your HTML and calling it when the page is loaded.

Your asp:ListBox must have AutoPostBack="True" if the selected_index_changed event should be raised by changing index.
for example
<asp:ListBox ID="ListBox1" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="selected_index_changed">
<asp:ListItem>one</asp:ListItem>
<asp:ListItem>two</asp:ListItem>
<asp:ListItem>three</asp:ListItem>
</asp:ListBox>'

public static void Alert(string message,Page page)
{
ScriptManager.RegisterStartupScript(page, page.GetType(),
"err_msg",
"alert('" + message + "');",
true);
}

Related

Adding blank row in gridview with button in footer

I have gridview with footer and addbutton on that, how to add new rows if I push the addbutton ? so I can enter new data on that. I also want save them all into database, can somebody explain please
this my front code:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" ShowFooter="True" Width="707px" >
<AlternatingRowStyle BackColor="White"/>
<Columns>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="AddProduct" runat="server" CssClass="button" Text="Add" onclick="AddProduct_Click" ></asp:Button>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField HeaderStyle-Width="120px" HeaderText="Id Trans" DataField="Id_Trans" ShowHeader="False" Visible="False">
<HeaderStyle Width="120px"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Nama Barang" SortExpression="Nama_Item">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="tampilbarang" DataTextField="Nama_Item" DataValueField="Id_Item">
</asp:DropDownList>
<asp:SqlDataSource ID="tampilbarang" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Item]"></asp:SqlDataSource>
</ItemTemplate>
<HeaderStyle Width="120px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="120px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Keterangan Penggunaan">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="120px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Aksi">
<ItemTemplate>
<asp:Button ID="Del" runat="server" Height="22px" Text="Delete" Width="57px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
There are a lot of things to explain in answer your question, so instead I'd recommend a good tutorial - try this - https://quickstarts.asp.net/quickstartv20/aspnet/doc/ctrlref/data/gridview.aspx
You can add a button in footer template like this example:
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Header 1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>

avoid reload gridview c# after clicking on a button

I created a web page Default6.aspx containing a gridview.
This gridview contains w column "radio button" (just one radio button can be selected)
I have a button that when I click,the Id of the row checked will be shown.
The problem that after click on the button,the checked radio button becomes unchecked and the gridview loads new data.
I couldn't really understand why.
Note: I have the same code in another Default5.aspx and it works.
this is the code of gridview:
<div style ="height:250px; width:1092px; overflow:auto;" spellcheck="True">
<asp:GridView ID="GridView2" runat="server"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" CellSpacing="4" GridLines="Vertical" Height="160px"
OnSelectedIndexChanged="GridView2_SelectedIndexChanged" Width="271px" ForeColor="Black"
ShowHeaderWhenEmpty="True" AutoPostBack="false">
<AlternatingRowStyle BackColor="White" />
<Columns >
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Main" >
<ItemTemplate>
<asp:RadioButton ID ="radioButton1" HeaderText="Main" runat="server"
onclick = "RadioCheck(this);" AutoPostBack ="False" Checked="False" CausesValidation="True" />
<asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("id")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("id") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#80FF00" Font-Bold="True" ForeColor="Black" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#CCCCCC" BorderStyle="Solid" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
</div>
and this is the code of the button:
<asp:Button ID="Button1" runat="server" BackColor="#80FF00" Font-Size="Large" OnClick="Button1_Click" Text="Delete unselected rows" Width="200px" />
for the test I created another button that just shows me a message "hello" when I click on it..and when I did (even I don't check a radio button),the gridview became refreshed..
what can be that?
thank you
Consider wrapping your button and the label in an updatepanel control, which uses AJAX to refresh their contents. The rest of the page will not be reloaded and the action will not affect the browser navigation.
the answer is:
if (!IsPostBack)
{ //my code}
else
{ //nothing to do}

can't access Textbox value which is in gridview control on button click

I have taken 3 text-boxes in grid view. I want to access all the 3 text-boxes value in single label on button click event.
ASP code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
CssClass="style1" ForeColor="#333333" GridLines="None" Height="342px" Width="548px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField ApplyFormatInEditMode="True" DataField="IName" HeaderText="Item Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="txtqty" runat="server" AutoPostBack="false" Text="Enter Quantity" />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtqtys" runat="server" AutoPostBack="false" OnTextChanged="TextBox_Changed"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="chkAll" runat="server" onclick="checkAll(this);" AutoPostBack="true"
OnCheckedChanged="CheckBox_CheckChanged" Text="Select" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick="Check_Click(this)" AutoPostBack="True"
OnCheckedChanged="CheckBox_CheckChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="true"></asp:Label>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />
C# code:
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
lbltxtqty.Visible = true;
lbltxtqty.Text = ((TextBox)GridView1.Rows[0].FindControl("txtqtys")).Text;
}
}
so please tell me the write code for it.
You are "Contradicting" your own aspx and code behind code. Why I am saying this is because, check you Label Mark-up & button click event handler:-
<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="true"></asp:Label>
Here you are setting labels Visible property to true which is of no use because its by default true. Now, see your button click event you are trying to make it visible by using lbltxtqty.Visible = true;, So obviously your mark-up should first look like this:-
<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="false"></asp:Label>
Because, you must be needing that when Page loads.
Now, comes your button click event. Do you know the Page Life Cycle of ASP.NET? When button click event is fired that's an obvious PostBack request so what is the Point of checking IsPostBack property there? Remove this code and it should work. Change your button click handler as follows:-
protected void btnsubmit_Click(object sender, EventArgs e)
{
lbltxtqty.Visible = true;
lbltxtqty.Text = ((TextBox)GridView1.Rows[0].FindControl("txtqtys")).Text;
}

gridview edit template validation

when i use template in edit mode with out avalidation control ,when user click edit
lins of grid turn to edit mode and page not send eny postback to server. but when i use
edit template with validation contorol ,the page sending postback to server and page is
reloads all content agaian. how can i fix so page will act like first one
<asp:GridView ID="gridProducts" BorderColor="Black" DataSourceID="sourceProducts" DataKeyNames="ProductID" AutoGenerateColumns="false"
AllowPaging="true" PageSize="3" AllowSorting="true" EnablePersistedSelection="true" runat="server">
<HeaderStyle BackColor="Black" ForeColor="White"/>
<RowStyle BackColor="Gray" ForeColor="White" />
<Columns>
<asp:BoundField HeaderText="ID" ReadOnly="true" SortExpression="ProductID" DataField="ProductID" />
<asp:BoundField HeaderText="NAME" SortExpression="ProductName" DataField="ProductName" />
<asp:BoundField HeaderText="ONE UNIT PRICE" DataFormatString="{0:c}" SortExpression="UnitPrice" DataField="UnitPrice" />
<asp:TemplateField HeaderText="STATUS">
<ItemStyle Width="100px" />
<ItemTemplate>
<b>In Srock:</b>
<%#Eval("UnitsInStock")%><br />
<b>On Order:</b>
<%#Eval("UnitsOnOrder")%><br />
<b>Reorder</b>
<%#Eval("ReorderLevel")%>
</ItemTemplate>
<EditItemTemplate>
<b>In Stock:</b><%#Eval("UnitsInStock")%><br />
<b>On Order:</b><%#Eval("UnitsOnOrder")%><br />
<asp:TextBox ID="txtReorder" runat="server" Text='<%# Bind("ReorderLevel") %>' Width="30px"></asp:TextBox>
<asp:RangeValidator ID="rngValidator" MinimumValue="0"
MaximumValue="100" ErrorMessage="Value Out Of Range" ControlToValidate="txtReorder" EnableClientScript="true" Type="Integer" runat="server">
</asp:RangeValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="true" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sourceProducts" runat="server"
ConnectionString="<%$ConnectionStrings:Northwind%>"
SelectCommand="SELECT ProductID,ProductName,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel
FROM Products WHERE CategoryID=#CategoryID"
UpdateCommand="UPDATE Products SET ProductName=#ProductName,UnitPrice=#UnitPrice,
ReorderLevel=#ReorderLevel WHERE ProductID=#ProductID">
<SelectParameters>
<asp:ControlParameter Name="CategoryID" ControlID="gridCategories" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
</asp:SqlDataSource>

MySql gridview delete command

I have this GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete"
onclientclick="javascript:return confirm('are you sure you want to delete.');"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"/>
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
<asp:BoundField DataField="first" HeaderText="first" SortExpression="first" />
<asp:BoundField DataField="last" HeaderText="last" SortExpression="last" />
<asp:BoundField DataField="gender" HeaderText="gender"
SortExpression="gender" />
<asp:BoundField DataField="birthday" HeaderText="birthday"
SortExpression="birthday" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
And this is the SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:clientConnectionString %>"
ProviderName="<%$ ConnectionStrings:clientConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM Client;"
DeleteCommand="DELETE FROM Client WHERE id = #id AND email = #email;">
<DeleteParameters>
<asp:Parameter Name="id" Type="String" />
<asp:Parameter Name="email" Type="String" />
</DeleteParameters>
</asp:SqlDataSource>
And when i try to excute a delete i get this error:
Parameter '#id' must be defined.
Exception Details: MySql.Data.MySqlClient.MySqlException: Parameter '#id' must be defined.
You may want to add the DataKeyNames attribute to your GridView
You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work. The values of these key fields are passed to the data source control in order to specify the row to update or delete. See this link
DataKeyNames="id,email"
In the context of your Gridview.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" DataKeyNames="id,email">

Categories

Resources