//dataGrid Define
<asp:datagrid id="DataGridNewsType" style="Z-INDEX: 107; LEFT: 56px; POSITION: absolute; TOP: 64px"
runat="server" Width="800px" Height="104px" CellPadding="0" AutoGenerateColumns="False" BorderColor="#4A3C8C"
HorizontalAlign="Justify" DataKeyField="newstypeid">
<SelectedItemStyle ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle Font-Size="X-Small" Height="20px" BackColor="Gainsboro"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Justify" ForeColor="White" Width="780px" BackColor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="newstypeid" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="当前新闻类别">
<ItemTemplate>
<%# Container.DataItem("newstypename")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="NTName" Text='<%# Container.DataItem("newstypename")%>' Runat ="server">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="删除类别">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<a href='DelType.aspx?Id=<%# Container.DataItem("newstypeid")%>'>删除</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="保存修改" CancelText="取消" EditText="编辑类别">
<HeaderStyle Width="200px"></HeaderStyle>
</asp:EditCommandColumn>
</Columns>
</asp:datagrid>
//Code-Behind Here
Private Sub DataGridNewsType_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridNewsType.UpdateCommand
Dim id As String
id = e.Item.Cells(0).Text.ToString()
Dim NewName As String
NewName = CType(e.Item.Cells(1).Controls(0), TextBox).Text.ToString()
LabMsg.Text = NewName
Dim ConnectionString As String = ConfigurationSettings.AppSettings("ConnectString")
Dim UpText As String = "UPDATE [newstype] SET newstypename="
UpText = UpText & "'" & NewName & "'"
UpText = UpText & " WHERE newstypeid='" & id & "'"
Dim UpConnection As New OleDbConnection(ConnectionString)
Dim UpCommand As New OleDbCommand(UpText, UpConnection)
Try
UpConnection.Open()
UpCommand.ExecuteNonQuery()
UpConnection.Close()
Catch err As Exception
LabMsg.Text = "发生异常:" & err.Message
Finally
If Not (UpConnection Is Nothing) Then
UpConnection.Close()
End If
End Try
DataGridNewsType.EditItemIndex = -1
'BindNewsType()
End Sub
<asp:datagrid id="DataGridNewsType" style="Z-INDEX: 107; LEFT: 56px; POSITION: absolute; TOP: 64px"
runat="server" Width="800px" Height="104px" CellPadding="0" AutoGenerateColumns="False" BorderColor="#4A3C8C"
HorizontalAlign="Justify" DataKeyField="newstypeid">
<SelectedItemStyle ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle Font-Size="X-Small" Height="20px" BackColor="Gainsboro"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Justify" ForeColor="White" Width="780px" BackColor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="newstypeid" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="当前新闻类别">
<ItemTemplate>
<%# Container.DataItem("newstypename")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="NTName" Text='<%# Container.DataItem("newstypename")%>' Runat ="server">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="删除类别">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<a href='DelType.aspx?Id=<%# Container.DataItem("newstypeid")%>'>删除</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="保存修改" CancelText="取消" EditText="编辑类别">
<HeaderStyle Width="200px"></HeaderStyle>
</asp:EditCommandColumn>
</Columns>
</asp:datagrid>
//Code-Behind Here
Private Sub DataGridNewsType_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridNewsType.UpdateCommand
Dim id As String
id = e.Item.Cells(0).Text.ToString()
Dim NewName As String
NewName = CType(e.Item.Cells(1).Controls(0), TextBox).Text.ToString()
LabMsg.Text = NewName
Dim ConnectionString As String = ConfigurationSettings.AppSettings("ConnectString")
Dim UpText As String = "UPDATE [newstype] SET newstypename="
UpText = UpText & "'" & NewName & "'"
UpText = UpText & " WHERE newstypeid='" & id & "'"
Dim UpConnection As New OleDbConnection(ConnectionString)
Dim UpCommand As New OleDbCommand(UpText, UpConnection)
Try
UpConnection.Open()
UpCommand.ExecuteNonQuery()
UpConnection.Close()
Catch err As Exception
LabMsg.Text = "发生异常:" & err.Message
Finally
If Not (UpConnection Is Nothing) Then
UpConnection.Close()
End If
End Try
DataGridNewsType.EditItemIndex = -1
'BindNewsType()
End Sub
“/newsManage”应用程序中的服务器错误。
--------------------------------------------------------------------------------指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 123: Dim UpConnection As New OleDbConnection(ConnectionString)
行 124: Dim UpCommand As New OleDbCommand(UpText, UpConnection)
行 125: Try
行 126: UpConnection.Open()
行 127: UpCommand.ExecuteNonQuery()
源文件: c:\inetpub\wwwroot\newsManage\LoginedIndex.aspx.vb 行: 125 堆栈跟踪:
[InvalidCastException: 指定的转换无效。]
newsManage.LoginedIndex.DataGridNewsType_UpdateCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\newsManage\LoginedIndex.aspx.vb:125
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1277
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
NewName = CType(e.Item.Cells(1).Controls(0), TextBox).Text.ToString()NewName = CType(e.Item.Cells(0).Controls(0), TextBox).Text.ToString()NewName = CType(e.Item.Cells(2).Controls(0), TextBox).Text.ToString()NewName = CType(e.Item.Cells(3).Controls(0), TextBox).Text.ToString()
????
这样的情况,出现在该单元格的第一个控件不一定就是你放入的控件,有时候,它会在里面放入一些其他控件以做其他事情。
你可用e.Item.Cells(1).Controls(0).GetType().toString()看一下它到底是什么控件
现已作了尝试,代码如下:// Content.aspx.cs ///////////////////////////////////////////////////////
public class Content : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Session.Timeout = 60;
Response.Write("欢迎 . . .<br>\n");
Response.Flush();
Application[Session.SessionID] = Response;
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
// Send.aspx.cs //////////////////////////////////////////////////////////
public class Send : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button ButtonSend;
protected System.Web.UI.WebControls.TextBox TextBox1;
private void ButtonSend_Click(object sender, System.EventArgs e)
{
foreach(string name in Application.AllKeys)
{
HttpResponse Response = Application[name] as HttpResponse;
if(Response!=null && Response.IsClientConnected)
{
Response.Write(TextBox1.Text + "<br>\n");
Response.Flush();
}
else
{
Application.Remove(name);
}
}
}
} 可以聊天,但发现一大问题:同时连接的用户被限制在50个左右,再多的就连不上了,并且会导致其他用户也陷于停滞状态。
请高手答疑解惑,还可另开贴给分!