Juan T. Llibre wrote:
I just realized you are trying to use an asp.net datagrid control,
but are using html <datagrid...> syntax.
You can't insert a textbox inside a datagrid unless
you declare it in code as an ItemTemplate.
That's why you're getting the "Name 'name' is not declared." error.
Try this, if it suits you.
<form runat="server">
<asp:datagrid id="dg" runat="server">
</asp:DataGrid>
<asp:TextBox id="name" runat="Server" />
<br/>
<%=name.ClientI D %>
</form>
If you want a textbox inserted inside a datagrid,
you'd do well to study Scott Mitchell's excellent datagrid tutorial :
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx
The 9th part of the tutorial explains how to do what you want to do:
http://aspnet.4guysfromrolla.com/articles/090902-1.aspx
*sigh*
Here I was hoping to save having to post nearly 300 lines of code by
providing a simplified illustration. Really, I didn't fall off the
turnip truck yesterday. I actually have a decent idea of what I should
be doing. I'm just missing a few pieces of the puzzle.
But since you've already thrown down the gauntlet, I hope you don't mind
putting your big mouth where your (probably even bigger) ego is and
going through my code to see what might be up.
Well, here goes:
<!-- begin code -->
<%@ Page Language="VB" Debug="true" %>
<% @Import Namespace="Syst em.Data" %>
<% @Import Namespace="Syst em.Data.OleDb" %>
<% @Import Namespace="Syst em.Configuratio n" %>
<% @Import Namespace="Syst em.Web.UI" %>
<% @Import Namespace="Syst em.Web.UI.WebCo ntrols" %>
<% @Import Namespace="Syst em.Web.UI.HtmlC ontrols" %>
<% @Import Namespace="Syst em.IO" %>
<% @Import Namespace="Syst em.Drawing" %>
<% @Import Namespace="Syst em.Drawing.Imag ing" %>
<%@ OutputCache Duration="20" VaryByParam="*" Location="None"
VaryByHeader="U ser-Agent"%>
<%@ Register TagPrefix="METZ " TagName="Meta" Src="/ssi/meta.ascx" %>
<%@ Register TagPrefix="METZ " TagName="Head" Src="/ssi/head.ascx" %>
<%@ Register TagPrefix="METZ " TagName="Foot" Src="/ssi/foot.ascx" %>
<METZ:Meta Id="ctlMeta" Runat="Server" />
<METZ:Head Id="ctlHead" Runat="Server" />
<script runat="server">
Sub Page_Load(sende r as Object, e as EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Sub BindData()
Dim myConn as New
OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
Dim myCmd as New OleDbCommand("S ELECT * FROM tblNews", myConn)
myConn.Open()
dg.DataSource = myCmd.ExecuteRe ader(CommandBeh avior.CloseConn ection)
dg.DataBind()
myConn.Close()
End Sub
Sub dg_Edit(sender As Object, e As DataGridCommand EventArgs)
dg.ShowFooter = False
dg.EditItemInde x = e.Item.ItemInde x
BindData()
End Sub
Sub dg_Update(sende r As Object, e As DataGridCommand EventArgs)
Dim imgContent as Object = e.Item.Cells(3) .Controls(1)
Dim imgStream As Stream = imgContent.Post edFile.InputStr eam()
Dim imgLen As Integer = imgContent.Post edFile.ContentL ength
Dim imgType as String = imgContent.Post edFile.ContentT ype
If Not imgStream Is Nothing And imgLen > 0 And (imgType =
"image/jpeg" Or imgType = "image/pjpeg") Then 'If new image was selected
for upload, then author intends to replace original image with new one
Dim imgBin() as Byte
imgBin = createThumbnail (imgStream, 200, 200)
Dim myConn as New
OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
Dim myCmd as New OleDbCommand("U PDATE tblNews SET [Date]=@Date,
[Title]=@Title, [Image]=@Image, [Comment]=@Comment WHERE [ID]=@ID", myConn)
myConn.Open()
myCmd.CommandTy pe = CommandType.Tex t
myCmd.Parameter s.Add("@Date", OleDbType.Date) .Value =
String.Format(" {0:dddd, dd MMMMM, yyyy}",
Convert.ToDateT ime(e.Item.Cell s(1).Controls(0 )))'CType(e.Ite m.Cells(1).Cont rols(0),
Textbox).Text
myCmd.Parameter s.Add("@Title", OleDbType.VarWC har).Value =
CType(e.Item.Ce lls(2).Controls (1), Textbox).Text.R eplace("'","’ ")
myCmd.Parameter s.Add("@Image", OleDbType.LongV arBinary).Value = imgBin
myCmd.Parameter s.Add("@Comment ", OleDbType.LongV arWChar).Value =
CType(e.Item.Ce lls(4).Controls (1), Textbox).Text.R eplace("'","’ ")
myCmd.Parameter s.Add("@ID", OleDbType.Integ er).Value =
e.Item.Cells(0) .Text
myCmd.ExecuteNo nQuery()
myConn.Close()
dg.ShowFooter = True
dg.EditItemInde x = -1
BindData()
Else 'If no new image was selected for upload, then author only
intends to update the name and/or comments
Dim myConn as New
OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
Dim myCmd as New OleDbCommand("U PDATE tblNews SET [Date]=@Date,
[Title]=@Title, [Comment]=@Comment WHERE [ID]=@ID", myConn)
myConn.Open()
myCmd.CommandTy pe = CommandType.Tex t
Dim strDate as String = CType(e.Item.Ce lls(1).Controls (0),
Textbox).Text
strDate = "#" & strDate & "#"
myCmd.Parameter s.Add("@Date", OleDbType.Date) .Value = "#" & strDate
& "#"'CType(e.Ite m.Cells(1).Cont rols(0), Textbox).Text
myCmd.Parameter s.Add("@Title", OleDbType.VarWC har).Value =
CType(e.Item.Ce lls(2).Controls (1), Textbox).Text.R eplace("'","’ ")
myCmd.Parameter s.Add("@Comment ", OleDbType.LongV arWChar).Value =
CType(e.Item.Ce lls(4).Controls (1), Textbox).Text.R eplace("'","’ ")
myCmd.Parameter s.Add("@ID", OleDbType.Integ er).Value =
e.Item.Cells(0) .Text
myCmd.ExecuteNo nQuery()
myConn.Close()
dg.ShowFooter = True
dg.EditItemInde x = -1
BindData()
End If
End Sub
Sub dg_Cancel(sende r As Object, e As DataGridCommand EventArgs)
dg.ShowFooter = True
dg.EditItemInde x = -1
BindData()
End Sub
Sub dg_Delete(sende r As Object, e As DataGridCommand EventArgs)
If e.CommandName = "Delete" Then
Dim myConn as New
OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
Dim myCmd as New OleDbCommand("D ELETE * From tblNews WHERE [ID] =
@ID", myConn)
myConn.Open()
myCmd.CommandTy pe = CommandType.Tex t
myCmd.Parameter s.Add("@ID", OleDbType.Integ er).Value =
dg.DataKeys(e.I tem.ItemIndex)
myCmd.ExecuteNo nQuery()
myConn.Close()
dg.ShowFooter = True
dg.EditItemInde x = -1
BindData()
End If
End Sub
Sub dg_Insert(sende r As Object, e As DataGridCommand EventArgs)
If e.CommandName = "Insert" Then
Dim imgContent as Object = e.Item.Cells(3) .Controls(1)
Dim imgStream As Stream = imgContent.Post edFile.InputStr eam()
Dim imgLen As Integer = imgContent.Post edFile.ContentL ength
Dim imgType as String = imgContent.Post edFile.ContentT ype
If Not imgStream Is Nothing And imgLen > 0 And (imgType =
"image/jpeg" Or imgType = "image/pjpeg") Then
Dim imgBin() as Byte
imgBin = createThumbnail (imgStream, 200, 200)
Dim myConn as New
OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
Dim myCmd as New OleDbCommand("I NSERT INTO tblNews ([Date],
[Title], [Image], [Comment]) VALUES (@Date, @Title, @Image, @Comment)",
myConn)
myConn.Open()
myCmd.CommandTy pe = CommandType.Tex t
myCmd.Parameter s.Add("@Date", OleDbType.Date) .Value =
CType(e.Item.Ce lls(1).Controls (0), Textbox).Text
myCmd.Parameter s.Add("@Title", OleDbType.VarWC har).Value =
CType(e.Item.Ce lls(2).Controls (1), Textbox).Text.R eplace("'","’ ")
myCmd.Parameter s.Add("@Image", OleDbType.LongV arBinary).Value =
imgBin
myCmd.Parameter s.Add("@Comment ", OleDbType.LongV arWChar).Value =
CType(e.Item.Ce lls(4).Controls (1), Textbox).Text.R eplace("'","’ ")
myCmd.ExecuteNo nQuery()
myConn.Close()
dg.EditItemInde x = -1
BindData()
Else
Dim myConn as New
OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
Dim myCmd as New OleDbCommand("I NSERT INTO tblNews ([Date],
[Title], [Comment]) VALUES (@Date, @Title, @Comment)", myConn)
myConn.Open()
myCmd.CommandTy pe = CommandType.Tex t
myCmd.Parameter s.Add("@Date", OleDbType.Date) .Value =
CType(e.Item.Ce lls(1).Controls (0), Textbox).Text
myCmd.Parameter s.Add("@Title", OleDbType.VarWC har).Value =
CType(e.Item.Ce lls(2).Controls (1), Textbox).Text.R eplace("'","’ ")
myCmd.Parameter s.Add("@Comment ", OleDbType.LongV arWChar).Value =
CType(e.Item.Ce lls(4).Controls (1), Textbox).Text.R eplace("'","’ ")
myCmd.ExecuteNo nQuery()
myConn.Close()
dg.EditItemInde x = -1
BindData()
End If
End If
End Sub
Private Function createThumbnail (ByVal ImageStream As Stream, ByVal
tWidth As Double, ByVal tHeight As Double) As Byte()
Dim g As System.Drawing. Image
=System.Drawing .Image.FromStre am(ImageStream)
Dim thumbSize As New Size()
thumbSize =NewthumbSize(g .Width, g.Height, tWidth, tHeight)
Dim imgOutput As New Bitmap(g, thumbSize.Width , thumbSize.Heigh t)
Dim imgStream As New MemoryStream()
Dim thisFormat = g.RawFormat
imgOutput.Save( imgStream, thisFormat)
Dim imgbin(imgStrea m.Length) As Byte
imgStream.Posit ion = 0
Dim intStatus as Integer = imgStream.Read( imgbin, 0, imgbin.Length)
g.Dispose()
imgOutput.Dispo se()
Return imgbin
End Function
Function NewthumbSize(By Val currentwidth As Double, ByVal currentheight
As Double, ByVal newWidth As Double, ByVal newHeight As Double)
Dim tempMultiplier As Double
If currentheight > currentwidth Then ' portrait
tempMultiplier = newHeight / currentheight
Else
tempMultiplier = newWidth / currentwidth
End If
Dim NewSize As New Size(CInt(curre ntwidth * tempMultiplier) ,
CInt(currenthei ght * tempMultiplier) )
Return NewSize
End Function
Function FormatData(sIte m) as String
FormatData=sIte m.Replace(vbcrl f,"<br />")
End Function
</script>
<div id="content">
<div class="floatrig ht">
<div id="timer"></div>
You are here: <a href="/default.aspx">H ome</a> » News & Info »
<b>News</b>
</div>
<h2>News - News Edit</h2>
<p>Here is where you add the latest news title, as well as an “eyecatch
photo” (something appropriately gorgeous) and a short description(a
paragraph or two) about the news.</p>
<p>Please Remember:</p>
<ul class="alert">
<li>If you are to <strong><em>EVE R</em></strong> delete a News entry,
please understand that everything in that entry, including photos and
their associated comments, will also be deleted. Irreversably. You have
been warned.</li>
<li>All fields except the Image field must be filled, otherwise you
will get a very abrupt and nasty error message from the server itself.
As a computer, it gets very cranky when it’s given stuff it can’t work
with. You have been warned.</li>
</ul>
<form id="Form1" method="post" runat="server"
enctype="multip art/form-data">
<asp:datagrid id="dg" runat="server"
showheader="tru e"
showfooter="tru e"
autogeneratecol umns="False"
edititemstyle-backcolor="#ffc ccc"
oneditcommand=" dg_Edit"
onupdatecommand ="dg_Update"
oncancelcommand ="dg_Cancel"
ondeletecommand ="dg_Delete"
onitemcommand=" dg_Insert"
datakeyfield="I D"
cellpadding="10 "
width="758">
<HeaderStyle backcolor="#000 000" forecolor="#fff fff" font-bold="true"
horizontalalign ="center" />
<ItemStyle backcolor="#fff fff" forecolor="#000 000" />
<AlternatingIte mStyle backcolor="#ccc ccc" />
<Columns>
<asp:BoundColum n DataField="ID" HeaderText="ID" ReadOnly="true"
Visible="false" />
<asp:TemplateCo lumn HeaderText="Dat e">
<FooterTemplate >
<asp:TextBox ID="add_Date" Text='<%# DateTime.Now.To String("dddd, dd
MMMM, yyyy") %>' Runat="Server" style="display: none;" />
<img src="/images/cal.png" alt="Choose Calendar Date"
id="calendar_tr igger" /> <span id="show_date"> <%=
DateTime.Now.To String("dddd, dd MMMM, yyyy") %></span>
<script type="text/javascript">
Calendar.setup( {
inputField : "", // id of the input field
displayArea : "show_date" , // ID of the span where
the date is to be shown
button : "calendar_trigg er", // trigger button
(well, IMG in our case)
align : "br", // alignment (defaults to "Bl")
singleClick : true
});
</script>
<!-- this actually fails with the error message -->
<%= add_Date.Client ID %>
</FooterTemplate>
<ItemTemplate >
<%# String.Format(" {0:dddd, dd MMMMM, yyyy}",
Convert.ToDateT ime(Container.D ataItem("Date") )) %>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="edit_Date" Text='<%# String.Format(" {0:dddd, dd MMMMM,
yyyy}", Convert.ToDateT ime(Container.D ataItem("Date") )) %>'
Runat="Server" style="display: none;" />
<img src="/images/cal.png" alt="Choose Calendar Date"
id="calendar_tr igger" /> <span id="show_date"> <%#
String.Format(" {0:dddd, dd MMMMM, yyyy}",
Convert.ToDateT ime(Container.D ataItem("Date") )) %></span>
<script type="text/javascript">
Calendar.setup( {
inputField : "", // id of the input field
displayArea : "show_date" , // ID of the span where
the date is to be shown
button : "calendar_trigg er", // trigger button
(well, IMG in our case)
align : "br", // alignment (defaults to "Bl")
singleClick : true
});
</script>
<!-- this actually fails with the error message -->
<%= edit_Date.Clien tID %>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Nam e">
<FooterTemplate >
<asp:TextBox ID="add_Title" MaxLength="50" Runat="Server" />
</FooterTemplate>
<ItemTemplate >
<%# Container.DataI tem("Title") %>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="edit_Title " MaxLength="50" Text='<%#
Container.DataI tem("Title") %>' Runat="server" />
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Ima ge">
<FooterTemplate >
<input type="file" id="add_Image" Runat="server" />
</FooterTemplate>
<ItemTemplate >
<img src="/displayimage.as px?table=tblNew s&id=<%#
Container.DataI tem("ID") %>" alt="Thumbnail" />
</ItemTemplate>
<EditItemTempla te>
<input type="file" id="edit_Image " Runat="server" />
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Com ment">
<FooterTemplate >
<asp:TextBox ID="add_Comment " Columns="30" Rows="4"
TextMode="Multi Line" Wrap="True" Runat="Server" />
</FooterTemplate>
<ItemTemplate >
<%# FormatData(Cont ainer.DataItem( "Comment")) %>
</ItemTemplate>
<EditItemTempla te>
<asp:TextBox ID="edit_Commen t" Columns="30" Rows="4"
TextMode="Multi Line" Wrap="True" Text='<%# Container.DataI tem("Comment")
%>' Runat="server" />
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:EditComman dColumn HeaderText="Edi t" EditText="Edit"
ButtonType="Pus hButton" UpdateText="Upd ate" CancelText="Can cel" />
<asp:TemplateCo lumn HeaderText="Del ete">
<FooterTemplate >
<asp:Button CommandName="In sert" Text="Add" ID="btnAdd"
Runat="server" />
</FooterTemplate>
<ItemTemplate >
<asp:Button CommandName="De lete" Text="Delete" ID="btnDel"
Runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign ="center" />
<FooterStyle HorizontalAlign ="center" />
</asp:TemplateCol umn>
</Columns>
</asp:datagrid><% = add_Date.Client ID %>
</form>
</div>
<METZ:Foot Id="ctlFoot" Runat="Server" />
<!-- End Code -->
Have fun.
...Geshel
--
*************** *************** *************** *************** **********
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
*************** *************** *************** *************** **********