月儿原创那学习了 GridView实现用“...”代替超长字符串 代码使用在了一个可编辑的列中,但是使用“...”代替超长字符串后,不能编辑了,点击编辑不会出现文本框,删除“...”代替超长字符串的代码后才可以,大侠们帮忙看看哪里出现的问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//引用命名空间
using System.Data;
using System.Data.OleDb;public partial class _Default : System.Web.UI.Page
{
//数据库连接字符串 public static string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/CODR_Database.mdb"); protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bind();
}
}
public void bind()
{
string sql;
sql = "select * from CaseList order by id,FINANCE_WEEK desc";
OleDbConnection conn = new OleDbConnection();//创建连接对象
conn.ConnectionString = connstring;//给连接字符串赋值
conn.Open();//打开数据库
OleDbDataAdapter Dar = new OleDbDataAdapter(sql, connstring); DataSet ds = new DataSet();
Dar.Fill(ds,"CaseList");
GridView1.DataSource = ds;
GridView1.DataBind(); //GridView实现用“...”代替超长字符串
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv;
string gIntro; if (GridView1.PageIndex == 0)
{
mydrv = ds.Tables["CaseList"].DefaultView[i];
gIntro = Convert.ToString(mydrv["REMARK_Simple"]);
GridView1.Rows[i].Cells[11].Text = SubStr(gIntro, 2);
gIntro.ToString();
}
else
{
mydrv = ds.Tables["CaseList"].DefaultView[i + (5 * GridView1.PageIndex)];
gIntro = Convert.ToString(mydrv["REMARK_Simple"]);
GridView1.Rows[i].Cells[11].Text = SubStr(gIntro, 2); }
}
conn.Close();//关闭数据库 }
//GridView实现用“...”代替超长字符串
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
//分页功能
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
//取消编辑状态
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
//进入编辑状态
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//数据更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int upid = (int)GridView1.DataKeys[e.RowIndex].Value;
string p = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
string u = ((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text; string str = "update CaseList set pod='" + p + "',REMARK_Simple='" + u + "' where id=" + upid; OleDbConnection conn = new OleDbConnection();//创建连接对象
conn.ConnectionString = connstring;//给连接字符串赋值
conn.Open();//打开数据库
OleDbCommand cmd = new OleDbCommand(str, conn);
cmd.ExecuteNonQuery();//
conn.Close();//关闭数据库
Response.Write("<script langeuage=javascript>;alert('更新成功!');</script>");
GridView1.EditIndex = -1;
bind();
}}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//引用命名空间
using System.Data;
using System.Data.OleDb;public partial class _Default : System.Web.UI.Page
{
//数据库连接字符串 public static string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/CODR_Database.mdb"); protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bind();
}
}
public void bind()
{
string sql;
sql = "select * from CaseList order by id,FINANCE_WEEK desc";
OleDbConnection conn = new OleDbConnection();//创建连接对象
conn.ConnectionString = connstring;//给连接字符串赋值
conn.Open();//打开数据库
OleDbDataAdapter Dar = new OleDbDataAdapter(sql, connstring); DataSet ds = new DataSet();
Dar.Fill(ds,"CaseList");
GridView1.DataSource = ds;
GridView1.DataBind(); //GridView实现用“...”代替超长字符串
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv;
string gIntro; if (GridView1.PageIndex == 0)
{
mydrv = ds.Tables["CaseList"].DefaultView[i];
gIntro = Convert.ToString(mydrv["REMARK_Simple"]);
GridView1.Rows[i].Cells[11].Text = SubStr(gIntro, 2);
gIntro.ToString();
}
else
{
mydrv = ds.Tables["CaseList"].DefaultView[i + (5 * GridView1.PageIndex)];
gIntro = Convert.ToString(mydrv["REMARK_Simple"]);
GridView1.Rows[i].Cells[11].Text = SubStr(gIntro, 2); }
}
conn.Close();//关闭数据库 }
//GridView实现用“...”代替超长字符串
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
//分页功能
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
//取消编辑状态
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
//进入编辑状态
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//数据更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int upid = (int)GridView1.DataKeys[e.RowIndex].Value;
string p = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
string u = ((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text; string str = "update CaseList set pod='" + p + "',REMARK_Simple='" + u + "' where id=" + upid; OleDbConnection conn = new OleDbConnection();//创建连接对象
conn.ConnectionString = connstring;//给连接字符串赋值
conn.Open();//打开数据库
OleDbCommand cmd = new OleDbCommand(str, conn);
cmd.ExecuteNonQuery();//
conn.Close();//关闭数据库
Response.Write("<script langeuage=javascript>;alert('更新成功!');</script>");
GridView1.EditIndex = -1;
bind();
}}
<cc1:datagrid id="DataGrid1" runat="server" PageSize="20" OnCancelCommand="DataGrid1_CancelCommand" OnDeleteCommand="DataGrid1_DeleteCommand" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand" onitemdatabound="DataGrid1_ItemDataBound">
<Columns>
<asp:BoundColumn DataField="id" HeaderText="ID" ReadOnly="true"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="店铺信息">
<ItemTemplate>
<%# Galsun.stgj.Bll.Shop.GetInfoByID(Convert.ToInt32(Eval("ShopID"))).Title%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="item_ddl_ShopID" runat="server">
</asp:DropDownList>
<asp:HiddenField ID="item_hf_ShopID" Value=<%# Eval("ShopID") %> runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="OrdNum" HeaderText="权重排序"></asp:BoundColumn>
<asp:BoundColumn DataField="title" HeaderText="图片标题"></asp:BoundColumn>
<asp:BoundColumn DataField="Content" HeaderText="说明"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="缩略图"><ItemTemplate><img src="<%# Eval("filename") %>" /></ItemTemplate></asp:TemplateColumn>
</Columns>
<PagerStyle Mode="NumericPages" />
<SelectedItemStyle CssClass="datagridSelectedItem" />
<HeaderStyle CssClass="category" />
</cc1:datagrid>
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
} protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindData();
} protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
int id = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]); string ShopID = e.Item.Cells[3].FindControl("item_ddl_ShopID").UniqueID;
string newnum = ((System.Web.UI.WebControls.TextBox)e.Item.Cells[4].Controls[0]).Text.Trim();
string newname = ((System.Web.UI.WebControls.TextBox)e.Item.Cells[5].Controls[0]).Text.Trim();
string newcont = ((System.Web.UI.WebControls.TextBox)e.Item.Cells[6].Controls[0]).Text.Trim();
albuminfo info = album.GetInfoByID(id);
info.ShopID = Discuz.Common.DNTRequest.GetInt(ShopID, 0);
info.OrdNum = newnum;
info.Title = newname;
info.Content = Discuz.Common.Utils.GetTextFromHTML(newcont);
info.modidate = DateTime.Now;
album.Update(info); DataGrid1.EditItemIndex = -1;
BindData(); } protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
int id = Discuz.Common.Utils.StrToInt(DataGrid1.DataKeys[e.Item.ItemIndex].ToString(), 0);
album.Delete(id);
DataGrid1.EditItemIndex = -1;
BindData();
}
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.Cells[3].FindControl("item_ddl_ShopID");
if (ddl != null)
{
foreach (ListItem listtiem in ddl_ShopID.Items)
{
ddl.Items.Add(listtiem);
}
HiddenField hf = (HiddenField)e.Item.Cells[3].FindControl("item_hf_ShopID");
ddl.Text = hf.Value;
}
}
如果是的话 建议楼主使用行绑定事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[6].Text.Length>48)
{
e.Row.Cells[6].ToolTip = e.Row.Cells[6].Text;
e.Row.Cells[6].Text = e.Row.Cells[6].Text.Substring(0,45)+"...";
}
else
{
e.Row.Cells[6].ToolTip = e.Row.Cells[6].Text;
}
if (e.Row.Cells[7].Text == "0" || e.Row.Cells[7].Text=="")
{ e.Row.Cells[7].Text = "未推荐";
}
else
{
e.Row.Cells[7].Text = "<font color='red'>已推荐</font>";
}
}
}
{
GridViewRow dv = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int index = dv.RowIndex;
lblname.Text = GridView1.Rows[index].Cells[1].Text;
txtUpVipTime.Text = GridView1.Rows[index].Cells[4].ToolTip;
}不知道对楼主是否有用呵呵。。
connstring 是不是有问题, 因该是 conn
<%#Eval("xxx").ToString().Length > 5 ? Eval("xxx").ToString().Substring(0,4)+"...":Eval("xxx")%>
前台代码在此,我在前台用了三元运算符就报错了<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="ID"
EnableModelValidation="True" GridLines="Vertical"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
PageSize="5" style="text-align: left" Width="1200px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="LINK_TYPE" HeaderText="Link_Type" ReadOnly="True" />
<asp:BoundField DataField="ORIG_ORDER_NUM" HeaderText="ORIG_Num"
ReadOnly="True" />
<asp:BoundField DataField="RIMS_REF" HeaderText="RIMS_REF" ReadOnly="True" />
<asp:BoundField DataField="Channel_Level_4" HeaderText="Channel_Level_4"
ReadOnly="True" />
<asp:BoundField DataField="SVC_ORDER_NUM" HeaderText="SVC_Num"
ReadOnly="True" />
<asp:BoundField DataField="FORM_CREATOR" HeaderText="Form_Creator"
ReadOnly="True" />
<asp:BoundField DataField="TASK_CREATED_BY" HeaderText="Created_BY"
ReadOnly="True" />
<asp:BoundField DataField="FINANCE_WEEK" HeaderText="Week"
ReadOnly="True" />
<asp:BoundField DataField="NET_COD" HeaderText="COD" ReadOnly="True" />
<asp:BoundField DataField="POD" HeaderText="POD" />
<asp:BoundField DataField="REMARK_Simple" HeaderText="REMARK" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" /> </Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>