eshusheng(.NET生疏) 请进

a791223 2003-07-29 12:42:22

<%@ import namespace="System.Data.SqlClient" %>
<%@ import namespace="System.Data" %>
<HTML>
<HEAD>
<script language="vb" runat="server">
private sSqlCon as string = "server=192.168.0.16;uid=sa;pwd=hoauoa;database=eomp"

public sub Page_Load(sender as Object, e as EventArgs)

if not IsPostBack then
Bind()
end if

end sub

private sub Bind()

dim SqlCon as new SqlConnection(sSqlCon)
dim SqlCmd as new StringBuilder()
' SqlCmd.Append("SELECT ProductName,UnitPrice,")
' SqlCmd.Append("UnitsInStock,UnitsOnOrder,")
SqlCmd.Append("select fcid,fcdhgs,fcfhgs,CONVERT(char(10), fcdate, 21) as vfcdate,fcch,fcbusno,fcneze,fcfhfq,fcdqgs,fcshdsef,fcpczsfe,fcfhzfzc,CONVERT(char(10), fczfdate, 21) as fczfdate,fcshzf,fcdate as dfcdate from fctj where fcdhgs='上海' and fcfhgs='温州' order by dfcdate desc ")
dim sda as new SqlDataAdapter(SqlCmd.ToString(),SqlCon)
dim ds as new DataSet()
sda.Fill(ds,"upfctj")
Dim objDataView As New DataView(ds.Tables("upfctj"))
if objDataView.count<=0 then
else
btnFirst.Text = "最首页"
btnPrev.Text = "前一页"
btnNext.Text = "下一页"
btnLast.Text = "最后页"
DGProducts.DataSource = ds.Tables("upfctj").DefaultView
DGProducts.DataBind()
end if
end sub

Sub pagechanged(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
Dim startIndex As Integer
startIndex = DGProducts.CurrentPageIndex * DGProducts.PageSize
DGProducts.CurrentPageIndex = e.NewPageIndex
bind()
' ShowStats()
End Sub

Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
'used by external paging UI
Dim arg As String = sender.CommandArgument

Select Case arg
Case "next"
If (DGProducts.CurrentPageIndex < (DGProducts.PageCount - 1)) Then
DGProducts.CurrentPageIndex += 1
End If
Case "prev"
If (DGProducts.CurrentPageIndex > 0) Then
DGProducts.CurrentPageIndex -= 1
End If
Case "last"
DGProducts.CurrentPageIndex = (DGProducts.PageCount - 1)
Case Else
'page number
DGProducts.CurrentPageIndex = System.Convert.ToInt32(arg)
End Select
Bind()
ShowStats()
End Sub

Sub ShowStats()
lblCurrentIndex.Text = "第 " + (DGProducts.CurrentPageIndex + 1).ToString() + " 页"
lblPageCount.Text = "总共 " +DGProducts.PageCount.ToString() + " 页"
End Sub



public sub DGProducts_Edit(sender as Object, e as DataGridCommandEventArgs)

DGProducts.EditItemIndex = e.Item.ItemIndex
Bind()

end sub




public sub DGProducts_Cancel(sender as Object, e as DataGridCommandEventArgs)

DGProducts.EditItemIndex = -1
Bind()

end sub

public sub DGProducts_Update(sender as Object, e as DataGridCommandEventArgs)
dim ufcdate as date
dim ufcch,ufcbusno as string
Dim ufcneze,ufcfhfq,ufcdqgs,ufcshdsef,ufcpczsfe,ufcfhzfzc,ufcshzf,ufcid,ufczfdate as string
ufcdate = CType(e.Item.Cells(1).Controls(0), TextBox).Text
ufcch = CType(e.Item.Cells(2).Controls(0), TextBox).Text
ufcbusno= CType(e.Item.Cells(3).Controls(0), TextBox).Text
ufcneze = Ctype(e.Item.Cells(4).Controls(0),TextBox).Text
ufcfhfq=Ctype(e.Item.Cells(5).Controls(0),TextBox).Text
ufcdqgs=Ctype(e.Item.Cells(6).Controls(0),TextBox).Text
ufcshdsef=Ctype(e.Item.Cells(7).Controls(0),TextBox).Text
ufcpczsfe=Ctype(e.Item.Cells(8).Controls(0),TextBox).Text
ufcfhzfzc=Ctype(e.Item.Cells(9).Controls(0),TextBox).Text
ufczfdate=Ctype(e.Item.Cells(10).controls(0),TextBox).Text
ufcshzf=Ctype(e.Item.Cells(11).controls(0),TextBox).Text
ufcid=Ctype(e.Item.Cells(12).Controls(0),Textbox).Text


dim sSqlCmd as new StringBuilder()
sSqlCmd.Append("UPDATE fctj ")
sSqlCmd.Append("SET fcdate ='"&ufcdate &"',")
sSqlCmd.Append("fcch = '"&ufcch &"',")
sSqlCmd.Append("fcbusno = '"&ufcbusno &"',")
sSqlCmd.Append("fcneze = "&ufcneze &", ")
sSqlCmd.Append("fcfhfq = "&ufcfhfq &", ")
sSqlCmd.Append("fcdqgs = "&ufcdqgs &",")
sSqlCmd.Append("fcshdsef = "&ufcshdsef &", ")
sSqlCmd.Append("fcpczsfe = "&ufcpczsfe &",")
sSqlCmd.Append("fcfhzfzc = "&ufcfhzfzc &",")
if ufczfdate="" then
sSqlCmd.Append("fczfdate = (null),")
else
'response.write ("456")
sSqlCmd.Append("fczfdate = '"&ufczfdate &"',")
end if

sSqlCmd.Append("fcshzf = "&ufcshzf &"")

sSqlCmd.Append(" WHERE fcid = "&ufcid &"")
'response.write (sSqlCmd.ToString)
'response.end
dim SqlCon as new SqlConnection(sSqlCon)
Dim SqlCmd as new SqlCommand(sSqlCmd.ToString(),SqlCon)
' SqlCmd.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar, 40))
' SqlCmd.Parameters("@ProductName").Value = sProductName
' SqlCmd.Parameters.Add(new SqlParameter("@UnitsInStock", SqlDbType.SmallInt))
' SqlCmd.Parameters("@UnitsInStock").Value = sUnitsInStock
' SqlCmd.Parameters.Add(new SqlParameter("@UnitsOnOrder", SqlDbType.SmallInt))
' SqlCmd.Parameters("@UnitsOnOrder").Value = sUnitsOnOrder
' SqlCmd.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.SmallInt))
' SqlCmd.Parameters("@ProductID").Value = sProductID

SqlCon.Open()
SqlCmd.ExecuteNonQuery()
SqlCon.Close()

DGProducts.EditItemIndex = -1
Bind()

end sub

</script>
</HEAD>

---------------------------------
1、现在出现的问题是因为我要修改某一记录必须要有关键字段,我用了序号字段fcid。
我在DataGrid里将这个字段隐藏就无法取出它的值,必须将这个字段显示出来才可以取出它的值,只有用这个值我才可以更新数据。但是我想隐藏这个字段。
2、我点击edit编辑后生成的textbox长度如何控制,超过datagrid的页面的显示范围,如何按制textbox 的长度。
3、生成的textbox我如何用javascript控制每一个textbox的输入值。
...全文
150 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
a791223 2003-07-29
  • 打赏
  • 举报
回复
代码太长,补全余下的

<body>
<table width="750" align="center">
<tr>
<td>
<form id="Form1" runat="server">
<asp:datagrid id="DGProducts" runat="server" OnUpdateCommand="DGProducts_Update" OnCancelCommand="DGProducts_Cancel" OnEditCommand="DGProducts_Edit" AutoGenerateColumns="False" Font-Size="14px" HeaderStyle-CssClass="productsHead" AlternatingItemStyle-BackColor="Tan" Gridlines="None" BorderWidth="0px" Width="750px" Cellpadding="4" PageSize="5" AllowPaging="True" onpageindexchanged="pagechanged" PagerStyle-Mode="NumericPages">
<FooterStyle HorizontalAlign="Right"></FooterStyle>
<HeaderStyle ForeColor="Beige" CssClass="productsHead" BackColor="#0099CC"></HeaderStyle>
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="fcid" HeaderText="序号">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="vfcdate" HeaderText="登记时间">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcch" HeaderText="发车序号">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcbusno" HeaderText="车号">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcneze" HeaderText="本次营运总额">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcfhfq" HeaderText="发货人付清">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcdqgs" HeaderText="发货人定期结算">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcshdsef" HeaderText="收货公司代收运费">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcpczsfe" HeaderText="本次租车费用">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcfhzfzc" HeaderText="发货公司支付租车运费">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fczfdate" HeaderText="付车费时间">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fcshzf" HeaderText="收货公司代收租车运费">
<HeaderStyle HorizontalAlign="Center" Width="62px"></HeaderStyle>
</asp:BoundColumn>
</Columns>
</asp:datagrid>
</td>
</tr>
<tr>
<td>
<asp:label id="lblPageCount" runat="server" Font-size="12px"></asp:label><asp:label id="lblCurrentIndex" runat="server" Font-size="12px"></asp:label><asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Font-size="8pt" CommandArgument="0" ForeColor="navy" Font-Name="verdana"></asp:linkbutton> 
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Font-size="8pt" CommandArgument="prev" ForeColor="navy" Font-Name="verdana"></asp:linkbutton> 
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Font-size="8pt" CommandArgument="next" ForeColor="navy" Font-Name="verdana"></asp:linkbutton> 
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Font-size="8pt" CommandArgument="last" ForeColor="navy" Font-Name="verdana"></asp:linkbutton>
</td>
</tr>
</FORM>
</table>
</body>
</HTML>
eshusheng 2003-07-29
  • 打赏
  • 举报
回复
不能直接写的,后台写了,aspx里面应该有相应的对应这个事件啊。

写的方法,在你的vb文件中,控件选:DGProducts,事件选ItemDataBound,然后贴如上代码。这样可以确保DGProducts中有对事件ItemDataBound的响应。即你的aspx中相应的代码应该为:(注意第二行)

<asp:datagrid id="DGProducts" runat="server"
OnItemDataBound="DGProducts_ItemDataBound"
OnUpdateCommand="DGProducts_Update" OnCancelCommand="DGProducts_Cancel" OnEditCommand="DGProducts_Edit" AutoGenerateColumns="False" Font-Size="14px" HeaderStyle-CssClass="productsHead" AlternatingItemStyle-BackColor="Tan" Gridlines="None" BorderWidth="0px" Width="750px" Cellpadding="4" PageSize="5" AllowPaging="True" onpageindexchanged="pagechanged" PagerStyle-Mode="NumericPages">
你再调试一下,我本机调试通过了才发给你的。
goody9807 2003-07-29
  • 打赏
  • 举报
回复
up
a791223 2003-07-29
  • 打赏
  • 举报
回复
修改文本框宽度时提示以下错误:
Handles子句需要withevents
我是将代码写入.vb文件中的
eshusheng 2003-07-29
  • 打赏
  • 举报
回复
添加如下事件解决问题二:注意只修改了某一个文本框。你可以修改。
Private Sub DGProducts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles Brand.ItemDataBound
Dim i As Int16 = 0

If e.Item.ItemType = ListItemType.EditItem Then
Dim tb As TextBox
tb = CType(e.Item.Cells(1).Controls(0), TextBox) '注意这里只修改了第二个文本框。
tb.Width = Unit.Parse("20px")
tb.Text = Server.HtmlDecode(tb.Text)

End If

End Sub
eshusheng 2003-07-29
  • 打赏
  • 举报
回复
第一个问题:数据绑定函数里面加一行,
DGProducts.DataKeyField = "fcid"
DGProducts.DataBind()

然后分别在public sub DGProducts_Update(sender as Object, e as DataGridCommandEventArgs)函数中取id值:
Dim sfcid As String = DGProducts.DataKeys(e.Item.ItemIndex)

delete函数里面类似。
其中sfcid 就是你隐藏取得你的id值了。

问题二,我再想想。

问题三,javascript我不是很熟悉,抱歉,用验证控件试试吧。
发帖
.NET社区

6.1w+

社区成员

.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
帖子事件
创建了帖子
2003-07-29 12:42
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。