简体   繁体   English

您可以定义一个函数来单击C#代码中定义的超链接来更改C#属性吗?

[英]Can you define a function to change a C# property on the click of an hyperlink defined in c# code?

I know that's impossible to do from an hyperlink defined in javascript, but I'm not sure if the ones defined on C# works the same way. 我知道用javascript中定义的超链接是不可能的,但是我不确定C#上定义的超链接是否以相同的方式工作。

I mean I have this code: 我的意思是我有以下代码:

HyperLink hlk;
...
hlk.NavigateUrl = url

I know I could define something like this: 我知道我可以这样定义:

hlk.Attributes.Add("onclick", "return somefunction()");

But as far as I know something like that would only work for client-side code and I want it to change something from the server side. 但据我所知,类似的方法仅适用于客户端代码,并且我希望它从服务器端进行更改。

Is there some way to do something like this with the things I'm mentioning? 有什么办法可以做我要提到的事情吗? As the Hyperlink is generated from C# and not javascript there might be a way... 由于超链接是从C#而不是JavaScript生成的,因此可能有一种方法...

You want to add an event Handler for the click event? 您要为click事件添加事件处理程序吗?

In C#, you need to do 在C#中,您需要做

Hyperlink hl;
// ... (setting of hyperlink)
hl.Click += HandlerFunction; // HandlerFunction is your method

And the parameter needed for a click event are object sender, RoutedEventArgs e 点击事件所需的参数是object sender, RoutedEventArgs e

Your server runs C# and communicates with a browser, right? 您的服务器运行C#并与浏览器通信,对吗? Anything that you create on the server-side will eventually be sent to the client's browser (html + javascript). 您在服务器端创建的所有内容最终都会发送到客户端的浏览器(html + javascript)。 it doesn't matter how was the hyperlink created (C#, Java, PHP) because it is converted to html and then sent away from the server. 超级链接(C#,Java,PHP)的创建方式无关紧要,因为将其转换为html,然后从服务器发送出去。

You should use AJAX to send commands to the server. 您应该使用AJAX将命令发送到服务器。

For example: 例如:

hlk.Attributes.Add("onclick", "doSomeAjax(url, command)");

And the doSomeAjax function on the client's side can look something like this (I used jQuery): 客户端的doSomeAjax函数可以看起来像这样(我使用jQuery):

function doSomeAjax(url, command) {
    $.ajax({
        type: "post",
        url: url,
        data: command
    });
}

The url should point to an address you will make on the server that is designated to get commands. 网址应指向您将在服务器上指定用于获取命令的地址。 the command can be anything you want, for example myVar=true , then on your server side simply look for a myVar variable and change it to true. 该命令可以是您想要的任何内容,例如myVar=true ,然后在服务器端只需查找myVar变量并将其更改为true。

This example is of course very vulnerable to many kinds of attacks! 这个例子当然很容易受到多种攻击!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM