Categories
asp.net 开发

GridView vs DataGrid

dotNet 2.0后,M$推荐使用GridView取代DataGrid(Comparing the GridView and DataGrid Web Server Controls)。在做Testlink测试Case的Chart,然后想在表中插入HTML format的时候出现了奇怪的问题。DataGrid一切正常,GridView貌似对直接插入的HTML进行了encode,HTML原始显示了出来。

狂搜一遍,

protected void gvTP_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Text = Server.HtmlDecode(e.Row.Cells[1].Text);
}
}

这下加入的HTML format就不会给encode咯。

Categories
asp.net 开发

GridView mouseover 的高亮

由于dotnet中获得到的RowStyle中color为System.Drawing.Color类的,所以首先需要将其转换成HTML能够接受的#xxxxxx形式。写了一个函数:

string toWebColor(System.Drawing.Color theColor)
{
  if (Convert.ToString(theColor.R, 16) == "0" && Convert.ToString(theColor.G, 16) == "0"
    && Convert.ToString(theColor.B, 16) == "0")
  {
    return "#ffffff";
  }
  else
  {
    return "#" + Convert.ToString(theColor.R, 16) + Convert.ToString(theColor.G, 16)
      + Convert.ToString(theColor.B, 16);
  }
}

下边的程序就是个间隔行设置Attribute的行为:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  if (e.Row.RowState == DataControlRowState.Normal)
  {
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" +
      toWebColor(GridView.RowStyle.BackColor) + "'");
  }
  else if (e.Row.RowState == DataControlRowState.Alternate)
  {
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" +
      toWebColor(GridView.AlternatingRowStyle.BackColor) + "'");
  }
  else
  {
    return;
  }
}
Categories
asp.net

GridView vs DataGrid, Server.HtmlDecode()

dotNet 2.0后,M$开始推荐使用GridView取代DataGrid(Comparing the GridView and DataGrid Web Server Controls)。前些天在做Testlink测试Case的Chart,然后想在表中插入HTML format的时候出现了奇怪的问题。DataGrid一切正常,GridView貌似对直接插入的HTML进行了encode,HTML被原原本本地显示了出来。 DataView Encoded 在MSDN上找到Server类下边有个HTML解码的方法Server.HtmlDecode(),题外话还有个Server.HtmlEncode()的方法。

protected void gvTestPlan_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    e.Row.Cells[1].Text = Server.HtmlDecode(e.Row.Cells[1].Text);
  }
}

这下加入的HTML format就不会给encode了。 DataView Decoded

Categories
asp.net

Autopostback提示“该对象不支持此属性或方法”(Object doesn’t support this property or method)

在公司调试没有问题,回家改了些许东西,居然DropDownList的Autopostback不起作用了。。。郁闷,google了下。。。 google到的一个解决方法:
问题找到了,原来是最后第二行有个 <asp:button id="submit" runat="server" text="添   加"></asp:button> <asp:button ..="" id="submit"> ====&gt; 变成 <asp:button..>了 晕死,后台里又有 protected System.Web.UI.WebControls.Button submit; 这个东西的,而且 submit 按钮功能一切正常,谁会想到是这个原因。 </asp:button..></asp:button>
和我的情况不符合,只能自己尝试了之了。一一比较两段程序,发现问题了。
  <asp:label forecolor="Aqua" id="Label1" runat="server" text="Label"></asp:label>
 

  Test Project:
  <asp:dropdownlist autopostback="true" id="listTestProj" runat="server"><pre lang="html4strict">  </pre></asp:dropdownlist> Test Plan: <asp:dropdownlist autopostback="true" id="listTestPlan" runat="server"> </asp:dropdownlist> <asp:button id="submit" onclick="Do_Click" runat="server" text="Submit"> </asp:button>
问题出在Button上,我定义其ID为submit,修改为Do就没有问题了。
<asp:label forecolor="Aqua" id="Label1" runat="server" text="Label"></asp:label>

  Test Project:
  <asp:dropdownlist autopostback="true" id="listTestProj" runat="server">
  </asp:dropdownlist>
 

  Test Plan:
  <asp:dropdownlist autopostback="true" id="listTestPlan" runat="server">
  </asp:dropdownlist>
 

  <asp:button id="Do" onclick="Do_Click" runat="server" text="Submit">
</asp:button></pre></div>
<p>估摸着应该是asp.net要求asp.net空间需要放在runat server的form,而form的提交默认为button的submit的缘故。莫名死了。。。</p>