[英]Clickable Webcontrol, ASP.NET
如果您需要更多背景信息,這是與我最后一個問題有關的問題 。
我的問題是:是否可以使asp.net表中的單元格可點擊?
還是至少可以在ASP.NET中制作可單擊的WebControl(應該可以將其放置在ControlCollection中),而不是Button或LinkButton? 如果不是,是否有可能在按鈕文本中添加多行信息?
我嘗試將其他組件添加到按鈕的ControlCollection中(我已經在Button的Windows窗體版本中看到了該組件),以查看是否可以將子組件呈現給按鈕,但沒有成功:
private void ModifyTableCell(TableCell cell)
{
//Create new button
Button btnCell = new Button();
btnCell.Click += (sender, args) =>
{
//Event for the button
};
//Create new Label
Label lblCell = new Label();
lblCell.Font.Bold = true;
lblCell.Text = "This text won't appear";
btnCell.Controls.Add(lblCell); //Attempt to add label to Button
cell.Controls.Add(btnCell);
}
編輯 :我最終只是為整個單元格創建了多行的LinkButton。
通過分配onclick
屬性並利用__doPostBack
函數,您應該幾乎可以使任何控件都可單擊。
ctrl.Attributes["onclick"] = string.Format("__doPostBack('{0}', '{1}');", ctrl.ClientID, "SomeArgument");
您也可以使用GetPostBackEventReference
方法。 此選項實際上更安全,因為它將注冊尚不存在的__doPostBack
函數:
ctrl.Attributes["onclick"] = Page.ClientScript.GetPostBackEventReference(ctrl, string.Empty);
然后,在后面的代碼中,您可以簡單地重寫RaisePostBackEvent
方法:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
base.RaisePostBackEvent(source, eventArgument);
if (eventArgument == "SomeArgument") //using the argument
{
//do whatever
}
}
您可以使用字符串生成器向asp.net按鈕添加多行,例如:
System.Text.StringBuilder buttonText = new System.Text.StringBuilder();
buttonText.AppendLine("first line");
buttonText.AppendLine("second line");
btnMultiline.Text = buttonText.ToString;
我不確定您如何想象將提供這種復合控件。 請記住,最后每個ASP.NET控件都輸出HTML。 您按鈕本質上輸出
<input type="button">The button text</input>
如果要在<input>
標記內放置其他內容,它必須與HTML兼容。 我不確定input標簽是否允許其他HTML放入其中。
另一方面,如果它是LinkButton,則生成的HTML標記是<a href="">
標記。 您可以在此處放置任何內容,甚至可以放置圖像,這些內容都可以單擊。
我不確定您的完整情況如何,但是您嘗試執行的操作聞起來很不好。 我建議您要么使用LinkButton要么重新考慮您的方法,但請記住HTML的最終輸出是什么。
在閱讀您的兩個帖子時,您似乎希望能夠單擊單元格中的任何內容並將其發布回,就像整個單元格都是一個按鈕一樣?
如果是這樣,一種相當簡單的方法就是像不嘗試發回郵件一樣構建單元格內容。 然后添加一個display:none;
樣式的按鈕display:none;
。 然后,您可以使用客戶端腳本捕獲單元格的click事件,並引發按鈕的click事件以引起回發。
這使您可以按照自己喜歡的任何方式創建單元格內容,並自動為您生成回發代碼和處理程序。
使用JQuery,您最終會得到如下結果:
<head runat="server">
<style>
input.ClickableCellButton
{
display: none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("td.ClickableCell").click(function (event) { $(event.target).children("input.ClickableCellButton").click() });
});
</script>
<div>
<table>
<tbody>
<tr>
<td class="ClickableCell">
Cell Contents<br />
on multiple lines
<asp:Button ID="Button1" runat="server" Text="Button" CssClass="ClickableCellButton"
OnClick="Button1_Click" />
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
如果不是,是否有可能在按鈕文本中添加多行信息?
對於這種特殊情況,您只能通過CSS來完成; 無需擴展Button:
<asp:button id="myMultilineButton" runat="server" style="width:60px; white-space: normal;" Text="Several lines of text" />
它將呈現為:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.