From your posted code, I think you need to correct followings:
1) Remove <htm, and place <html> tag after <!DOCTYPE
tag
2) SelectCommand of datasource need to modify. It should be select rec_id, column and not select
rec_idrec, 3) From your table structure, id column is identity, so no need to insert
4) Because you are selecting record based on parameter @rec_id and it is default to 0. If there is no sv_recid2
session defined, it will show EmptyItemTemplate, which is 2 textboxes. ** I think you need to add an
Add button there.
I use code below, and I think it display correctly.
Many many thanks Kpyap. I don't find out <htm, after DOCTYPE. Now I have the following codes (pls note that now @rec_id will not affect the output of the GV) but I still don't see the records even if there're records in the table (pls see this)
<asp:SqlDataSource
ID="ds_det" Runat="server"
SelectCommand="select rec_id as rec,CONVERT(varchar,fld,105) fld from t where 0<>isnull(@rec_id,1) order by convert(varchar,fld,102)"
i expect that, once i've clicked 'new' button, the set of available buttons will be 'Save', 'Cancel', then I can edit for the new record and am able to further decide to save/give up the record.
I think you can remove New button on every row, because your purpose is to display a single blank row which is editable.
I think you can do that by making use of FooterTemplate, by adding 'Save' and 'Cancel' button at FooterTemplate.
So I change CommandField to TemplateField as below:
Many many thanks Kpyap. Yes, here for Save, command is assigned with "Insert"
<FooterTemplate>
<asp:TextBox runat="server" ID="nw_fld" HtmlEncode="False"
Enabled="true" Width="65px" Text='<%# Eval("fld") %>' />
<asp:LinkButton id="linkbutton1" runat="server" CausesValidation="true" command="Insert" Text="Save"></asp:LinkButton>
<asp:LinkButton id="linkbutton2" runat="server" CausesValidation="false"
command="Cancel" Text="Cancel" OnClick="cancel_Click" ></asp:LinkButton>
</FooterTemplate>
but within this event - 'gv_ins' I checked that the value of e.CommandName is nothing. You can also refer to these sources. Great appreciations in advance!
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Dec 03, 2009 12:47 AM|LINK
Hi,
From your posted code, I think you need to correct followings:
1) Remove <htm, and place <html> tag after <!DOCTYPE tag
2) SelectCommand of datasource need to modify. It should be select rec_id, column and not select rec_id rec,
3) From your table structure, id column is identity, so no need to insert
4) Because you are selecting record based on parameter @rec_id and it is default to 0. If there is no sv_recid2 session defined, it will show EmptyItemTemplate, which is 2 textboxes. ** I think you need to add an Add button there.
I use code below, and I think it display correctly.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>temp</title> </head> <body> <form id="form1" runat="server" BackColor="AntiqueWhite" > <asp:GridView ID="gv3" runat="server" AutoGenerateColumns="false" HeaderStyle-BorderStyle="Double" BorderWidth="2px" Onrowupdating="gv_edit" Onrowdeleting="gv_del" OnRowCommand="gv_ins" Font-Size="Small" ForeColor="DarkBlue" BackColor="AntiqueWhite" BorderStyle="Groove" AllowSorting="True" title="My records" DataKeyNames="rec_id" DataSourceID="ds_det" > <HeaderStyle BackColor="#aaaadd"> </HeaderStyle> <Columns> <asp:BoundField DataField="rec_id" HeaderText="Rec ID" HtmlEncode="False" > <HeaderStyle HorizontalAlign="Left" width="65px" /> <ItemStyle HorizontalAlign="Left" width="65px" /> </asp:BoundField> <asp:CommandField ShowInsertButton="true" ShowEditButton="true" ShowDeleteButton="true" /> <asp:TemplateField HeaderText="Rec ID" HeaderStyle-HorizontalAlign="Left" > <ItemTemplate> <asp:TextBox runat="server" ID="itm_fld" HtmlEncode="False" ForeColor="DarkBlue" BackColor="AntiqueWhite" BorderStyle="None" ReadOnly="true" Width="65px" Text='<%# Eval("fld") %>'/> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" ID="edi_fld" HtmlEncode="False" Enabled="true" Width="65px" Text='<%# Eval("fld") %>' /> </EditItemTemplate> <FooterTemplate> <asp:TextBox runat="server" ID="nw_fld" HtmlEncode="False" Enabled="true" Width="65px" Text='<%# Eval("fld") %>' /> </FooterTemplate> </asp:TemplateField> </Columns> <EmptyDataTemplate> rec<asp:TextBox runat="server" ID="nwrec" /> fld<asp:TextBox runat="server" ID="nwfld" /> </EmptyDataTemplate> </asp:GridView> <asp:SqlDataSource ID="ds_det" Runat="server" SelectCommand="select rec_id,CONVERT(varchar,fld,105) fld from t where rec_id=@rec_id order by convert(varchar,fld,102)" UpdateCommand="update t set fld=CONVERT(datetime,@fld,105) where rec_id=@rec_id" InsertCommand="insert into t(fld) values (CONVERT(datetime, @fld, 105))" ConnectionString="<%$ ConnectionStrings:Mssqlconn %>" ProviderName="System.Data.SqlClient" > <SelectParameters> <asp:SessionParameter Name="rec_id" SessionField="sv_recid2" Type="Int32" Direction="Input" DefaultValue="0"/> </SelectParameters> <UpdateParameters> <asp:SessionParameter Name="rec_id" SessionField="sv_rec" Type="String" Direction="Input" DefaultValue=" "/> <asp:SessionParameter Name="fld" SessionField="sv_fld" Type="String" Direction="Input" DefaultValue=" "/> </UpdateParameters> <InsertParameters> <asp:SessionParameter Name="fld" SessionField="sv_fld" Type="String" Direction="Input" DefaultValue=" "/> </InsertParameters> </asp:SqlDataSource> <div> </div> </form> </body> </html>Hope it help.
wmec
Contributor
6224 Points
3221 Posts
Re: Onrowcreated
Dec 03, 2009 04:12 AM|LINK
Many many thanks Kpyap. I don't find out <htm, after DOCTYPE. Now I have the following codes (pls note that now @rec_id will not affect the output of the GV) but I still don't see the records even if there're records in the table (pls see this)
select *
from t;
1 2009-11-21 00:00:00.000
2 2009-11-24 00:00:00.000
3 2009-12-01 00:00:00.000
4 2009-12-02 00:00:00.000
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="temp.aspx.cs" Inherits="NewSite.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>temp</title>
</head>
<body>
<form id="form1" runat="server" BackColor="AntiqueWhite" >
<asp:GridView ID="gv3" runat="server"
AutoGenerateColumns="false"
HeaderStyle-BorderStyle="Double"
BorderWidth="2px"
Onrowupdating="gv_edit"
Onrowdeleting="gv_del"
OnRowCommand="gv_ins"
Font-Size="Small"
ForeColor="DarkBlue"
BackColor="AntiqueWhite"
BorderStyle="Groove"
AllowSorting="True"
title="My records"
DataKeyNames="rec_id"
DataSourceID="ds_det" >
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<Columns>
<asp:BoundField DataField="rec_id" HeaderText="Rec ID" HtmlEncode="False" >
<HeaderStyle HorizontalAlign="Left" width="65px" />
<ItemStyle HorizontalAlign="Left" width="65px" />
</asp:BoundField>
<asp:CommandField ShowInsertButton="true" ShowEditButton="true" ShowDeleteButton="true" />
<asp:TemplateField HeaderText="Rec ID" HeaderStyle-HorizontalAlign="Left" >
<ItemTemplate>
<asp:TextBox runat="server" ID="itm_fld" HtmlEncode="False" ForeColor="DarkBlue" BackColor="AntiqueWhite" BorderStyle="None"
ReadOnly="true" Width="65px" Text='<%# Eval("fld") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="edi_fld" HtmlEncode="False"
Enabled="true" Width="65px" Text='<%# Eval("fld") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="nw_fld" HtmlEncode="False"
Enabled="true" Width="65px" Text='<%# Eval("fld") %>' />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
rec<asp:TextBox runat="server" ID="nwrec" />
fld<asp:TextBox runat="server" ID="nwfld" />
</EmptyDataTemplate>
</asp:GridView>
<html
<asp:SqlDataSource
ID="ds_det" Runat="server"
SelectCommand="select rec_id as rec,CONVERT(varchar,fld,105) fld from t where 0<>isnull(@rec_id,1) order by convert(varchar,fld,102)"
UpdateCommand="update t set fld=CONVERT(datetime,@fld,105) where rec_id=@rec_id"
InsertCommand="insert into t(fld) values (cONVERT(datetime,@fld,105))"
ConnectionString="<%$ ConnectionStrings:Mssqlconn %>"
ProviderName="System.Data.SqlClient" >
<SelectParameters>
<asp:SessionParameter Name="rec_id" SessionField="sv_recid2" Type="Int32" Direction="Input" DefaultValue="0"/>
</SelectParameters>
<UpdateParameters>
<asp:SessionParameter Name="rec_id" SessionField="sv_rec" Type="String" Direction="Input" DefaultValue=" "/>
<asp:SessionParameter Name="fld" SessionField="sv_fld" Type="String" Direction="Input" DefaultValue=" "/>
</UpdateParameters>
<InsertParameters>
<asp:SessionParameter Name="rec_id" SessionField="sv_rec" Type="String" Direction="Input" DefaultValue=" "/>
<asp:SessionParameter Name="fld" SessionField="sv_fld" Type="String" Direction="Input" DefaultValue=" "/>
</InsertParameters>
</asp:SqlDataSource>
<div>
</div>
</form>
</body>
</html>
HuaMin Chen
wmec
Contributor
6224 Points
3221 Posts
Re: Onrowcreated
Dec 04, 2009 03:45 AM|LINK
Any advice?
HuaMin Chen
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Dec 08, 2009 12:41 AM|LINK
Above is the broken html tag you posted.
If you do not set the session, I think it will be default to 0. And look at the SelectCommand, it will return zero record.
Above code shown that only textboxes are displayed if no record bind to GridView
You can refer to my code posted and try to add Insert button inside <EmptyDataTemplate> as below:
<EmptyDataTemplate> rec<asp:TextBox runat="server" ID="nwrec" /> fld<asp:TextBox runat="server" ID="nwfld" /> <asp:Button runat="server" ID="btnInsert" Text="Insert" CommandName="Insert" /> </EmptyDataTemplate>wmec
Contributor
6224 Points
3221 Posts
Re: Onrowcreated
Dec 22, 2009 01:53 AM|LINK
Many many thanks Kpyap. I did add 'Insert' button like what you've suggested. Now I want to ask why it will automatically insert one record once I've clicked the button - 'New' there. there're 3 buttons like 'Edit', 'Delete', & 'New'. Could you pls refer to the following links to see the sources?
http://www.4shared.com/file/178457698/ef86493b/temp.html
http://www.4shared.com/file/178457622/eca779ee/tempaspx.html
i expect that, once i've clicked 'new' button, the set of available buttons will be 'Save', 'Cancel', then I can edit for the new record and am able to further decide to save/give up the record.
HuaMin Chen
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Dec 22, 2009 10:52 AM|LINK
Hi,
I think you can remove New button on every row, because your purpose is to display a single blank row which is editable.
I think you can do that by making use of FooterTemplate, by adding 'Save' and 'Cancel' button at FooterTemplate.
So I change CommandField to TemplateField as below:
<asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton> </ItemTemplate> <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> <FooterTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" Text="Save"></asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> </FooterTemplate> </asp:TemplateField>To handle New button, you can toggle ShowFooter property of GridView, as below:
protected void btnNew_Click(object sender, EventArgs e) { gv3.ShowFooter = true; }*Notes: New button is not inside GridView. It should be only ONE New button.
And then, I modified gv_ins to handle 'Save' button in FooterTemplate, as below:
protected void gv_ins(object sender, GridViewCommandEventArgs e) { ClientScript.RegisterClientScriptBlock(Page.GetType(), "alert", "alert('xxxyy! " + e.CommandName + " ');", true); if (e.CommandName.Trim() == "Insert") { ClientScript.RegisterClientScriptBlock(Page.GetType(), "alert", "alert('xxxyy!');", true); ds_det.Insert(); gv3.ShowFooter = false; } }Code is not tested, hope it help.
wmec
Contributor
6224 Points
3221 Posts
Re: Onrowcreated
Dec 28, 2009 06:09 AM|LINK
Many many thanks Kpyap. Is it unnecessary to check this?
if (e.CommandName.Trim() == "Insert")
since it seems the value of e.CommandName is empty when triggering gv_ins?
HuaMin Chen
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Dec 29, 2009 12:50 AM|LINK
Hi,
e.CommandName should has value "Insert" when gv_ins is triggered, because of this declaration:
<FooterTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" Text="Save"></asp:LinkButton>
wmec
Contributor
6224 Points
3221 Posts
Re: Onrowcreated
Dec 29, 2009 02:42 AM|LINK
Many many thanks Kpyap. Yes, here for Save, command is assigned with "Insert"
<FooterTemplate>
<asp:TextBox runat="server" ID="nw_fld" HtmlEncode="False"
Enabled="true" Width="65px" Text='<%# Eval("fld") %>' />
<asp:LinkButton id="linkbutton1" runat="server" CausesValidation="true"
command="Insert" Text="Save"></asp:LinkButton>
<asp:LinkButton id="linkbutton2" runat="server" CausesValidation="false"
command="Cancel" Text="Cancel" OnClick="cancel_Click" ></asp:LinkButton>
</FooterTemplate>
but within this event - 'gv_ins' I checked that the value of e.CommandName is nothing. You can also refer to these sources. Great appreciations in advance!
http://www.4shared.com/file/183639789/79284715/_2__tempaspx.html
http://www.4shared.com/file/183639697/86493164/_2__temp.html
HuaMin Chen
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Dec 29, 2009 04:41 AM|LINK
Hi,
The property name should be CommandName instead of Command.