我有一个bootstrap模式,显示标题列表,每个标题都有一个删除按钮。我创建了一个小草图,以明确我在说什么。
该表是使用Repeater创建的:
<asp:Repeater ID="rpRelated" runat="server" OnItemDataBound="rpRelated_ItemDataBound">
<HeaderTemplate>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th style="width: 20%">Title</th>
<th style="width: 10%">Delete</i></th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr runat="server" id="related">
<td>
<asp:HiddenField runat="server" ID="titleRelatedID" Value='<%# DataBinder.Eval(Container.DataItem , "ID")%>' />
<asp:TextBox runat="server" ID="relatedTitle" Text='<%# DataBinder.Eval(Container.DataItem , "Title")%>' CssClass="form-control" />
</td>
<td>
<a class="my-red-button" href='javascript:hideRelated(<%# DataBinder.Eval(Container.DataItem , "ID")%>);'>Delete</i></a>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
我在Repeater的<tr>
方法中设置相关的ItemDataBound
ID,以使其动态化,以便我可以分辨隐藏哪一行(Source of the Idea):< / p>
protected void rpRelated_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Title title = (Title)e.Item.DataItem;
var row = e.Item.FindControl("related");
row.ClientIDMode = ClientIDMode.Static;
row.ID = "related" + title.ID;
}
}
当按下其中一个删除按钮(链接)时,将调用以下JS函数:
function hideRelated(num) {
document.getElementById('related' + num).style.display = 'none';
}
此功能暂时隐藏行,但实际上并未删除标题。
在按下底部的SAVE按钮之前,我不希望任何标题被删除。单击SAVE按钮时,将在CodeBehind中调用以下Click事件,这就是我遇到的问题:
protected void btnSaveRelated_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in rpRelated.Items)
{
int ID = Convert.ToInt32(((HiddenField)item.FindControl("titleRelatedID")).Value);
var a = item.FindControl("related" + ID); //returns null
if (???title row is hidden???)
{
//code to actually delete the Title goes here
}
}
}
当我遍历Repeater项目时,如何判断相应的<tr>
是否具有display:none
属性?
我尝试通过item.FindControl("related" + ID);
访问该项的行,但这似乎无法工作,因为它总是返回null(可能是因为ClientIDMode是静态的)。我使用HiddenField
就像使用titleRelatedID
一样,但是当我点击其中一个按钮时,我无法弄清楚如何在JavaScript代码中更新正确的值。我也可能使用Visible=false
代替display:none
,但我无法弄清楚如何在JavaScript中使用此方法。
我很乐意让我现有的方法发挥作用,但我愿意采用完全不同的方法来完成我的任务:
答案 0 :(得分:2)
一种方法是使用另一个跟踪已删除标题ID的HiddenField控件:
btnSaveRelated_Click
在foreach (string id in this.deletedRelatedIDs.Value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
{
// Delete the title with this ID.
}
JavaScript函数中,将参数追加到隐藏字段:
btnSaveRelated_Click
在<tr>
方法中,读取隐藏字段的值:
display:none
安全注意事项:用户可以轻松篡改隐藏字段的内容。如有必要,item.FindControl("related" + ID)
方法应验证每个ID是否有效以及用户是否有权删除标题。
当我遍历Repeater项目时,如何判断相应的
"related"
是否具有Computer: Does the animal you're thinking of have legs? Player: Yes Computer: Is it a dog? Player: Yes Computer: I win! Do you want to play again? Player: Yes Computer: Does the animal you're thinking of have legs Player: Yes Computer: Is it a dog? Player: No Computer: I give up. What was your animal? Player: Horse Computer: Type a question which the answer is yes for Dog but No for Horse Player: Does it live in a house? Computer: Do you want to play again? Player: Yes Computer: Does the animal you're thinking of have legs? Player: Yes Computer: Does it live in a house? Player: No Computer: Is it a horse? Player: No Computer: I give up
属性?
你不能。当浏览器将表单提交给服务器时,服务器不会收到CSS属性的值。
为什么
import java.util.*; public class DoesItHaveLegs { public static void main(String[] args) { ArrayList<String> questions = new ArrayList<String>(); ArrayList<String> animals = new ArrayList<String>(); Scanner input = new Scanner(System.in); questions.add("Does it have legs"); animals.add("dog"); String userAnimal; String userQuestion = ""; String giveUp = "I give up. What animal was it?"; String userAnswer = "YES"; while(userAnswer.equals("YES")) { System.out.println(animals); System.out.println(questions); int q = 0; int a = 0; while (q < questions.size()) { System.out.println(questions.get(q)); userAnswer = input.nextLine().toUpperCase(); if(userAnswer.equals("YES")) { System.out.println("Is it a " + animals.get(a)); userAnswer = input.nextLine().toUpperCase(); while(a < animals.size()) { if(userAnswer.equals("YES")) { System.out.println("Yay! I win. Do you want to play again?"); userAnswer = input.nextLine().toUpperCase(); } else if(a < animals.size()) { a++; } else { System.out.println("I give up. What animal is it?"); userAnimal = input.nextLine(); animals.add(userAnimal); } } } else { if(q < questions.size()) { q++; } } } System.out.println("I give up. What animal is it?"); userAnimal = input.nextLine(); animals.add(userAnimal); System.out.println("Type in a question for which the answer is yes for " + animals.get(a) + " but no for " + userAnimal); userQuestion = input.nextLine(); questions.add(userQuestion); System.out.println("Do you want to play again?"); userAnswer = input.nextLine().toUpperCase(); } } }
总是返回null?
与大多数ASP.NET服务器控件属性不同,ID属性不由视图状态支持,因此不会在回发之间保留对ID属性的更改。相反,ID将恢复为标记({{1}})中指定的值。