有時候人傻起來,連自己都覺得可怕。
今天需要做一個很簡單的查詢修改的功能,在老的系統(tǒng)上加一個新的功能,C#,原本想用jQuery運用ajax動態(tài)加載的,突然修改DOM不會了,瞬間尷尬了。嚇得的我趕緊百度了一波。。。
但想想還是覺得用匹配一點的webform會好一點,于是就用了GridView控件了。
由于好久沒寫了,所以基本忘光了。
在gridview中綁定了一個textbox,
用((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()
獲取textbox控件中的值時報錯,說是:無法將類型為
“System.Web.UI.LiteralControl”的對象強制轉換為類型“System.Web.UI.WebControls.TextBox”,
將上面的寫法改為:
((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox1"))).Text.ToString().Trim()后,
又報錯說是“未將對象的引用設置到對象的實例” ,最后將其改為:
((TextBox)(GridView1.Rows[i].Cells[1].Controls[0]).FindControl("TextBox1")).Text.Trim ().ToString ()),
運行成功!想了一下,確實有道理,
(TextBox)(GridView1.Rows[i].Cells[1].Controls[0])是強制類型轉換,
轉換為textbox控件,后面再跟findcontrol(),找到特定的textbox1控件,然后獲取其值。
日了狗了,
前兩天也是做一個表格,也想用ajax的方式向后臺發(fā)請求,然請求發(fā)成功了,但后臺也可以發(fā)數(shù)據(jù),但前臺獲取不到,查了一下bootstrap table插件需要特定的json格式的數(shù)據(jù),但我后臺也是傳json,百度一天,無果。先放一放,等有時間把他個挖透來。
還是用GridView寫的
今天就把完整的GridView增刪改查給寫全來,省的忘記,不過確實忘記了,昨天花了一下午才搞定還加班到八點。
好了!牢騷發(fā)完了,開始寫代碼吧!
前臺代碼框奉上
<asp:GridView ID="GridView1" runat="server" CellPadding="3" ForeColor="Black"
GridLines="Vertical" AutoGenerateColumns="False" ShowFooter="True"
Width="99%" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
//這里的方法對應后臺的方法
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
Height="151px" onrowcancelingedit="GridView1_RowCancelingEdit"
OnPageIndexChanging="GridView1_PageIndexChanging"
AllowPaging="True" onrowdeleting="GridView1_RowDeleting" >
<Columns>
//因為需要編輯修改,所以用Templatefield
<asp:TemplateField HeaderText="題目ID(不可改)">
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="題目類型">
<EditItemTemplate>
<asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("topictype") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label13" runat="server" Text='<%# Bind("topictype") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
后臺代碼
//查詢
protected void 查詢(object sender, EventArgs e)
{
if (txtSkuno.Text.Trim() == "")
{
ClientScript.RegisterStartupScript(GetType(), "message", "alert('Input can not be null!');", true);
return;
}
string strSkuno = txtSkuno.Text.Trim().ToUpper();
string strSql =SQL語句;
DataTable dt = Common.getDataBySQL(strSql);
Session["TaskTable"] = dt;
//把查到的數(shù)據(jù)存到Session里,以便后續(xù)編輯
BindData();
}
//綁定數(shù)據(jù)
private void BindData()
{
GridView1.DataSource = Session["TaskTable"];
}
//分頁 前臺需要把AllowPaging設為true,設置兩次
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
btnSearchBySku_Click(e,e);
}
//修改
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = ((Label)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).FindControl("Label12")).Text.Trim().ToString();
string topictype = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).FindControl("TextBox13")).Text.Trim().ToString();
問題?:需要退回到上一個界面//現(xiàn)在的問題是退回到上一個界面但是沒有刷新數(shù)據(jù)
GridView1.EditIndex = -1;
//btnSearchBySku_Click(e, e);
//Bind data to the GridView control.
BindData();
}
//編輯
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
//取消編輯
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
//刪除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ID = ((Label)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).FindControl("Label12")).Text.Trim().ToString();
string strSql =刪除語句
DataTable ds = Common.getDataBySQL(strSql);
Response.Write("<script language='JavaScript'>");
Response.Write("alert('刪除成功!');");
Response.Write("</script>");
}