想要实现的功能,在编辑行的时候自动把当前的用户名写入数据库
目前我用response测试显示((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text,正确显示当前用户名,
但是并没有写入到数据库 ,在点了编辑按钮的时候,对应的单元格数据也没变 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text = (string)Session["UserName"];
} 同时想问下,如果我想把一个值给单元格,请问是在那个语句里面写protected void GridView1_RowUpdating里面写了好像不对
目前我用response测试显示((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text,正确显示当前用户名,
但是并没有写入到数据库 ,在点了编辑按钮的时候,对应的单元格数据也没变 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text = (string)Session["UserName"];
} 同时想问下,如果我想把一个值给单元格,请问是在那个语句里面写protected void GridView1_RowUpdating里面写了好像不对
不要用((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text
FindControl是如何找的呢
((TextBox)GridView1.Rows[i].Cells[0].FindControl('TextBox的ID')).Text.ToString()。置于重新绑定,是指你的更新已经插入到数据库了,那么你就要重新取一次数据绑定到GridView上,才能体现出数据的变化。。
直接用的gridvew控件
上次做出来次,但是代码忘记了,以前的文件给删除了
在点击更新后直接就更新进去了
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
//GridViewRow row = GridView1.Rows[e.RowIndex];//((TextBox)(row.Controls[1].Controls[0])).Text;
GridViewRow row = GridView1.Rows[GridView1.EditIndex];
//Label labObjScoreNum1 = e.Row.FindControl("labObjScoreNum1") as Label ; labObjScoreNum1.text
Label lab=row.Controls[14].FindControl("Label1") as Label ; string id = lab.Text.ToString(); string
_xm = ((TextBox)(row.Controls[1].Controls[0])).Text.ToString();
string
_IDC = ((TextBox)(row.Controls[2].Controls[0])).Text.ToString();
string
_Area = ((TextBox)(row.Controls[3].Controls[0])).Text.ToString();
string
_Street = ((TextBox)(row.Controls[4].Controls[0])).Text.ToString();
string
_committee = ((TextBox)(row.Controls[5].Controls[0])).Text.ToString();
string
_address = ((TextBox)(row.Controls[6].Controls[0])).Text.ToString();
string
_telephone = ((TextBox)(row.Controls[7].Controls[0])).Text.ToString();
string
_Doctor = ((TextBox)(row.Controls[8].Controls[0])).Text.ToString();
string
_Count = ((TextBox)(row.Controls[9].Controls[0])).Text.ToString(); string strSql = "update a set";
Run.RunSql(strSql);
}
catch (Exception ex)
{
Response.Write(ex);
} GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
代码主要我不太会,当时只需要一行,忘记加载到那里的了
((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text = (string)Session["UserName"];
这句的作用就是让,我选中行[e.rowindex]的列cells[4]的值是 session传送来的用户名
然后点更新,更新就可以了
你要不把你的代码贴出来看看吧,,实在是不明白你的意思!~~
代码如下页面文件;
<%@ Page Language="C#" MasterPageFile="~/User2/MasterPage.master" AutoEventWireup="true" CodeFile="CaseLV.aspx.cs" Inherits="User2_CaseLV" Title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p>
<asp:Button ID="Button3" runat="server" Text="未处理" />
<asp:Button ID="Button4" runat="server" Text="已处理"
PostBackUrl="~/User2/CASELV2.aspx" />
</p>
<p>
<asp:TextBox ID="TextBox1" runat="server" Width="78px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="日期" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="确定" />
</p>
<p>
<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC"
BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest"
Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="87px"
onselectionchanged="Calendar1_SelectionChanged" ShowGridLines="True"
Visible="False" Width="57px">
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<SelectorStyle BackColor="#FFCC66" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt"
ForeColor="#FFFFCC" />
</asp:Calendar>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
DataKeyNames="id" onrowupdating="GridView1_RowUpdating" >
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowEditButton="True" EditText="选择" UpdateText="确定" />
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"
InsertVisible="False" ReadOnly="True" Visible="False">
</asp:BoundField>
<asp:BoundField DataField="caseid" HeaderText="caseid"
SortExpression="caseid" >
<ItemStyle Width="300px" />
</asp:BoundField>
<asp:BoundField DataField="aggent" HeaderText="aggent"
SortExpression="aggent" Visible="False" />
<asp:BoundField DataField="folt2" HeaderText="folt2"
SortExpression="folt2" Visible="False" />
<asp:BoundField DataField="folt" HeaderText="folt"
SortExpression="folt" />
<asp:BoundField DataField="casetime" HeaderText="升级时间"
SortExpression="casetime" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</p>
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MuserConnectionString2 %>"
DeleteCommand="DELETE FROM [caselv] WHERE [id] = @id"
InsertCommand="INSERT INTO [caselv] ([caseid], [aggent], [folt2], [folt], [casetime]) VALUES (@caseid, @aggent, @folt2, @folt, @casetime)"
SelectCommand="SELECT [id], [caseid], [aggent], [folt2], [folt], [casetime] FROM [caselv] WHERE (([casetime] > @casetime) AND ([folt2] = @folt2))"
UpdateCommand="UPDATE [caselv] SET [caseid] = @caseid, [aggent] = getdate(), [folt2] = '已处理', [folt] = @folt, [casetime] = @casetime WHERE [id] = @id">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="casetime" PropertyName="Text"
Type="DateTime" />
<asp:Parameter DefaultValue="未处理" Name="folt2" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="caseid" Type="String" />
<asp:Parameter Name="aggent" Type="DateTime" />
<asp:Parameter Name="folt2" Type="String" />
<asp:Parameter Name="folt" Type="String"/>
<asp:Parameter Name="casetime" Type="DateTime" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="caseid" Type="String" />
<asp:Parameter Name="aggent" Type="DateTime" />
<asp:Parameter Name="folt2" Type="String" />
<asp:Parameter Name="folt" Type="String" />
<asp:Parameter Name="casetime" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
<br />
</p>
</asp:Content>
其实我的目的是
我使用gridview显示一个表里面的数据
我想实现,在选中一行点编辑的时候,将最后一行绑定的不是数据库的内容,绑定的当前用户名,我用的session保存的用户名
然后在点更新,将绑定的内容更新到数据库上
使用的是VS2008自带的控件器请帮助下谢谢
列如 数据库内标为 列1 列2 列3
1 12 3
2 22 23 通过此操作后(选择编辑,然后点击更新)后表如下
列1 列2 列3
1 12 tom
2 22 23
(tom为当前用户名,通过session在登录时保存的)