开发者

ASP, Listview conditional Alert

I'm currently working with a listview in which I want an htmltablecell to possess the oncli开发者_运维百科ck property which is driven by the codebehind rather than a javascript.. However I'm guessing that's pretty much a dream getting it to obey the C# code... Anyways this is what I want it to run:

    protected void show_anm(object sender, EventArgs e)
    {
        Label hiddenc = (Label)listview1.FindControl("hidden");
        Alert.Show(hiddenc.Text);
    }

and here's the Alert class

public static class Alert
{
    public static void Show(string message)
    {
        string cleanMessage = message.Replace("'", "\\'");
        string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";

        Page page = HttpContext.Current.CurrentHandler as Page;

        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
        }
    }
}

The point is creating a listview with a two conditional tablecells, one which appears only when a certain condition is met and the other appears every other time (that's alredy sorted out). Where the one demanding a condition is Clickable, and upon clicking it it'll display an Alertbox with Data from a specific DB cell...

Sorry if my language and the question seemes off, English isn't my native language and I haven't doused myself in Coffe yet.

Any help on the matter would be most appritiated

EDIT1*

<asp:Listview ................
    <ItemTemplate>
         <tr ......>
              <td id=default .....>
                    <asp:label ........ Text='<%# eval("stuff") %> />
              </td>
              <td id=conditional onclick=alert()..........>
                    <asp:label ......... Text='<%# eval("stuff") %> />
              </td>
              <td id=hidden visible=false ...........>
                    <asp:label ......... Text='<%#eval("stuff i want in alert") %>' />
              .....

<script tyupe="text/javascript">
function alert()
{
  var msg = document.getElementById("tried with label id and tablecell id nothing seemingly worked").value;
  alert(msg);
}
</script>

I recently made a workaround that shows the data I want to display in the labels tooltip but I'd still prefer the alertbox to work properly as it feels more natural to click something.

Edit2 In case anyone is wondering I used the ItemDataBound event to bind the visibility of cells default and conditional within an if clause to make sure the control exists and the conditions are met.


I am confused as to why you're doing what you're doing. Why do you want the codebehind to handle an onclick event of a htmltablecell when you are pumping out javascript to show an alert anyway?

Why not just handle the whole logic within Javascript?

A postback from a htmltablcell will also require javascript

Set your tablecell to call a javascript function which would obtain the alert text from the hidden value and display that;

        function ShowAlert()
        {
           var message = document.getElementbyId("hidden").value;
           alert.show(message);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜