我有一个GridView,可以从数据库中提取数据并自动填充其字段。
<asp:GridView ID="gvData" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" AllowPaging="True" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" DataSourceID="DS" PageSize="1" CssClass="q">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" Height="20" CssClass="q"/>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" Height="15px" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="DS" runat="server"></asp:SqlDataSource>
有些字段太长了。有没有办法缩短文本的长度并插入一个弹出一个带文本的新窗口的按钮? gridview aslo会生成“编辑”和“删除”按钮。按下编辑按钮给我一些texbox。如何使用弹出窗口编辑相同的按钮来解决它们?
我想到的另一种方法是使用滚动条创建textarea。我知道如何手动添加带滚动条的字段,但它是作为标签创建的,所以当我按下编辑时,此字段不可编辑。同样,它是手动的,但我的gridview是自动填充的。如何使用带有滚动条的textareas自动填充大字段?
任何帮助将不胜感激!谢谢!
好的,所以我找到了缩短文字的解决方案:
protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
ViewState["OrigData"] = e.Row.Cells[i].Text;
if (e.Row.Cells[i].Text.Length >= 30)
{
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Substring(0, 30) + "...";
e.Row.Cells[i].ToolTip = ViewState["OrigData"].ToString();
}
}
}
}
然后添加OnRowDataBound =“gvData_RowDataBound”:
<asp:GridView ID="gvData" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AllowPaging="True" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" DataSourceID="DS" PageSize="1"
OnRowDataBound="gvData_RowDataBound">
这会将长度缩短为30个字符,并在工具提示中显示全文。
答案 0 :(得分:1)
看一下这篇文章,我认为它可能有用。
答案 1 :(得分:1)
回答第二部分:
protected void gvData_PreRender(object sender, EventArgs e)
{
if (this.gvData.EditIndex != -1)
{
TextBox tb = new TextBox();
for (int i = 0; i < gvData.Rows[gvData.EditIndex].Cells.Count; i++)
try
{
tb = (TextBox)
gvData.Rows[gvData.EditIndex].Cells[i].Controls[0];
if (tb.Text.Length >= 30)
{
tb.TextMode = TextBoxMode.MultiLine;
}
}
catch { }
}
}