Juan said:
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
ataGrid>
<asp:TextBox id="name" runat="Server" />
<br/>
<%=name.ClientID %>
</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="System.Data" %>
<% @Import Namespace="System.Data.OleDb" %>
<% @Import Namespace="System.Configuration" %>
<% @Import Namespace="System.Web.UI" %>
<% @Import Namespace="System.Web.UI.WebControls" %>
<% @Import Namespace="System.Web.UI.HtmlControls" %>
<% @Import Namespace="System.IO" %>
<% @Import Namespace="System.Drawing" %>
<% @Import Namespace="System.Drawing.Imaging" %>
<%@ OutputCache Duration="20" VaryByParam="*" Location="None"
VaryByHeader="User-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(sender as Object, e as EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Sub BindData()
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("SELECT * FROM tblNews", myConn)
myConn.Open()
dg.DataSource = myCmd.ExecuteReader(CommandBehavior.CloseConnection)
dg.DataBind()
myConn.Close()
End Sub
Sub dg_Edit(sender As Object, e As DataGridCommandEventArgs)
dg.ShowFooter = False
dg.EditItemIndex = e.Item.ItemIndex
BindData()
End Sub
Sub dg_Update(sender As Object, e As DataGridCommandEventArgs)
Dim imgContent as Object = e.Item.Cells(3).Controls(1)
Dim imgStream As Stream = imgContent.PostedFile.InputStream()
Dim imgLen As Integer = imgContent.PostedFile.ContentLength
Dim imgType as String = imgContent.PostedFile.ContentType
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(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblNews SET [Date]=@Date,
[Title]=@Title, [Image]=@Image, [Comment]=@Comment WHERE [ID]=@ID", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
String.Format("{0:dddd, dd MMMMM, yyyy}",
Convert.ToDateTime(e.Item.Cells(1).Controls(0)))'CType(e.Item.Cells(1).Controls(0),
Textbox).Text
myCmd.Parameters.Add("@Title", OleDbType.VarWChar).Value =
CType(e.Item.Cells(2).Controls(1), Textbox).Text.Replace("'","’")
myCmd.Parameters.Add("@Image", OleDbType.LongVarBinary).Value = imgBin
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
CType(e.Item.Cells(4).Controls(1), Textbox).Text.Replace("'","’")
myCmd.Parameters.Add("@ID", OleDbType.Integer).Value =
e.Item.Cells(0).Text
myCmd.ExecuteNonQuery()
myConn.Close()
dg.ShowFooter = True
dg.EditItemIndex = -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(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblNews SET [Date]=@Date,
[Title]=@Title, [Comment]=@Comment WHERE [ID]=@ID", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
Dim strDate as String = CType(e.Item.Cells(1).Controls(0),
Textbox).Text
strDate = "#" & strDate & "#"
myCmd.Parameters.Add("@Date", OleDbType.Date).Value = "#" & strDate
& "#"'CType(e.Item.Cells(1).Controls(0), Textbox).Text
myCmd.Parameters.Add("@Title", OleDbType.VarWChar).Value =
CType(e.Item.Cells(2).Controls(1), Textbox).Text.Replace("'","’")
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
CType(e.Item.Cells(4).Controls(1), Textbox).Text.Replace("'","’")
myCmd.Parameters.Add("@ID", OleDbType.Integer).Value =
e.Item.Cells(0).Text
myCmd.ExecuteNonQuery()
myConn.Close()
dg.ShowFooter = True
dg.EditItemIndex = -1
BindData()
End If
End Sub
Sub dg_Cancel(sender As Object, e As DataGridCommandEventArgs)
dg.ShowFooter = True
dg.EditItemIndex = -1
BindData()
End Sub
Sub dg_Delete(sender As Object, e As DataGridCommandEventArgs)
If e.CommandName = "Delete" Then
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("DELETE * From tblNews WHERE [ID] =
@ID", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@ID", OleDbType.Integer).Value =
dg.DataKeys(e.Item.ItemIndex)
myCmd.ExecuteNonQuery()
myConn.Close()
dg.ShowFooter = True
dg.EditItemIndex = -1
BindData()
End If
End Sub
Sub dg_Insert(sender As Object, e As DataGridCommandEventArgs)
If e.CommandName = "Insert" Then
Dim imgContent as Object = e.Item.Cells(3).Controls(1)
Dim imgStream As Stream = imgContent.PostedFile.InputStream()
Dim imgLen As Integer = imgContent.PostedFile.ContentLength
Dim imgType as String = imgContent.PostedFile.ContentType
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(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("INSERT INTO tblNews ([Date],
[Title], [Image], [Comment]) VALUES (@Date, @Title, @Image, @Comment)",
myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
CType(e.Item.Cells(1).Controls(0), Textbox).Text
myCmd.Parameters.Add("@Title", OleDbType.VarWChar).Value =
CType(e.Item.Cells(2).Controls(1), Textbox).Text.Replace("'","’")
myCmd.Parameters.Add("@Image", OleDbType.LongVarBinary).Value =
imgBin
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
CType(e.Item.Cells(4).Controls(1), Textbox).Text.Replace("'","’")
myCmd.ExecuteNonQuery()
myConn.Close()
dg.EditItemIndex = -1
BindData()
Else
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("INSERT INTO tblNews ([Date],
[Title], [Comment]) VALUES (@Date, @Title, @Comment)", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
CType(e.Item.Cells(1).Controls(0), Textbox).Text
myCmd.Parameters.Add("@Title", OleDbType.VarWChar).Value =
CType(e.Item.Cells(2).Controls(1), Textbox).Text.Replace("'","’")
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
CType(e.Item.Cells(4).Controls(1), Textbox).Text.Replace("'","’")
myCmd.ExecuteNonQuery()
myConn.Close()
dg.EditItemIndex = -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.FromStream(ImageStream)
Dim thumbSize As New Size()
thumbSize =NewthumbSize(g.Width, g.Height, tWidth, tHeight)
Dim imgOutput As New Bitmap(g, thumbSize.Width, thumbSize.Height)
Dim imgStream As New MemoryStream()
Dim thisFormat = g.RawFormat
imgOutput.Save(imgStream, thisFormat)
Dim imgbin(imgStream.Length) As Byte
imgStream.Position = 0
Dim intStatus as Integer = imgStream.Read(imgbin, 0, imgbin.Length)
g.Dispose()
imgOutput.Dispose()
Return imgbin
End Function
Function NewthumbSize(ByVal 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(currentwidth * tempMultiplier),
CInt(currentheight * tempMultiplier))
Return NewSize
End Function
Function FormatData(sItem) as String
FormatData=sItem.Replace(vbcrlf,"<br />")
End Function
</script>
<div id="content">
<div class="floatright">
<div id="timer"></div>
You are here: <a href="/default.aspx">Home</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>EVER</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="multipart/form-data">
<asp:datagrid id="dg" runat="server"
showheader="true"
showfooter="true"
autogeneratecolumns="False"
edititemstyle-backcolor="#ffcccc"
oneditcommand="dg_Edit"
onupdatecommand="dg_Update"
oncancelcommand="dg_Cancel"
ondeletecommand="dg_Delete"
onitemcommand="dg_Insert"
datakeyfield="ID"
cellpadding="10"
width="758">
<HeaderStyle backcolor="#000000" forecolor="#ffffff" font-bold="true"
horizontalalign="center" />
<ItemStyle backcolor="#ffffff" forecolor="#000000" />
<AlternatingItemStyle backcolor="#cccccc" />
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" ReadOnly="true"
Visible="false" />
<asp:TemplateColumn HeaderText="Date">
<FooterTemplate>
<asp:TextBox ID="add_Date" Text='<%# DateTime.Now.ToString("dddd, dd
MMMM, yyyy") %>' Runat="Server" style="display: none;" />
<img src="/images/cal.png" alt="Choose Calendar Date"
id="calendar_trigger" /> <span id="show_date"><%=
DateTime.Now.ToString("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_trigger", // 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.ClientID %>
</FooterTemplate>
<ItemTemplate>
<%# String.Format("{0:dddd, dd MMMMM, yyyy}",
Convert.ToDateTime(Container.DataItem("Date"))) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="edit_Date" Text='<%# String.Format("{0:dddd, dd MMMMM,
yyyy}", Convert.ToDateTime(Container.DataItem("Date"))) %>'
Runat="Server" style="display: none;" />
<img src="/images/cal.png" alt="Choose Calendar Date"
id="calendar_trigger" /> <span id="show_date"><%#
String.Format("{0:dddd, dd MMMMM, yyyy}",
Convert.ToDateTime(Container.DataItem("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_trigger", // 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.ClientID %>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<FooterTemplate>
<asp:TextBox ID="add_Title" MaxLength="50" Runat="Server" />
</FooterTemplate>
<ItemTemplate>
<%# Container.DataItem("Title") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="edit_Title" MaxLength="50" Text='<%#
Container.DataItem("Title") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Image">
<FooterTemplate>
<input type="file" id="add_Image" Runat="server" />
</FooterTemplate>
<ItemTemplate>
<img src="/displayimage.aspx?table=tblNews&id=<%#
Container.DataItem("ID") %>" alt="Thumbnail" />
</ItemTemplate>
<EditItemTemplate>
<input type="file" id="edit_Image" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Comment">
<FooterTemplate>
<asp:TextBox ID="add_Comment" Columns="30" Rows="4"
TextMode="MultiLine" Wrap="True" Runat="Server" />
</FooterTemplate>
<ItemTemplate>
<%# FormatData(Container.DataItem("Comment")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="edit_Comment" Columns="30" Rows="4"
TextMode="MultiLine" Wrap="True" Text='<%# Container.DataItem("Comment")
%>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn HeaderText="Edit" EditText="Edit"
ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" />
<asp:TemplateColumn HeaderText="Delete">
<FooterTemplate>
<asp:Button CommandName="Insert" Text="Add" ID="btnAdd"
Runat="server" />
</FooterTemplate>
<ItemTemplate>
<asp:Button CommandName="Delete" Text="Delete" ID="btnDel"
Runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
<FooterStyle HorizontalAlign="center" />
</asp:TemplateColumn>
</Columns>
</asp:datagrid><%= add_Date.ClientID %>
</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.
**********************************************************************