简体   繁体   English

asp.net从javascript传递值以控制模式弹出窗口

[英]asp.net pass value from javascript to control on a modal popup

Ok, I changed the title because I can't get anywhere with previous approach, so I'm returning to the original question: how can I pass a variable obtained through javascript to a textbox on a modal popup? 好的,我更改了标题,因为以前的方法无法解决任何问题,因此我回到了原来的问题:如何将通过javascript获得的变量传递给模式弹出窗口中的文本框? I already tried to place a hidden field and even a textbox on the parent page, inside or outside an update panel, but when I click on the linkbutton that opens the modal popup their values are resetted to default. 我已经尝试在更新面板的内部或外部的父页面上放置一个隐藏字段,甚至是一个文本框,但是当我单击打开模式弹出窗口的链接按钮时,它们的值将重置为默认值。 I already searched and tried many different ways but I can't succeed. 我已经搜索并尝试了许多不同的方法,但是我无法成功。

I have a table in a repeater and I need to know the cells selected by the user: start and ending cell of the selection. 我在转发器中有一个表格,我需要知道用户选择的单元格:选择的开始和结束单元格。 I accomplish that with this javascript: 我使用此javascript完成此操作:

$(function () {
            var mouse_down = false;
            var row, col;    // starting row and column
            var $tr;

            $("#tblPersonale td")
                .mousedown(function () {
                    $("#col_to").html('?');
                    mouse_down = true;

                    // clear last selection for a fresh start
                    $(".highlighted").removeClass("highlighted");
                    $(this).addClass("highlighted");

                    $tr = $(this).parent();
                    row = $tr.parent().find("tr").index($(this).parent());
                    col = $tr.find("td").index($(this));

                    $("#row").html(row);
                    $("#col_fr").html(col - 1);



                    return false; // prevent text selection
                })

                .mouseover(function () {
                    if (mouse_down) {
                        $("#col_to").html('?');
                        if ($tr[0] === $(this).parent()[0]) {
                            var col2 = $(this).parent().find("td").index($(this)); // current column
                            var col1 = col;
                            if (col > col2) { col1 = col2; col2 = col; }

                            $("#col_fr").html(col1-1);
                            $("#col_to").html(col2 - 1);

                            // clear all selection to avoid extra cells selected
                            $(".highlighted").removeClass("highlighted");

                            // then select cells from col to col2
                            for (var i = col1; i <= col2; i++) {
                                if (col1>1){
                                    $tr[0].cells[i].className = "highlighted";}
                            }
                        }

                    }
                })
                .bind("selectstart", function () {
                    return false; // prevent text selction in IE
                })

            $(document)
                .mouseup(function () {
                    mouse_down = false;
                });
        });

So, when user selects one or more cells I have the start/end value in here: 因此,当用户选择一个或多个单元格时,我在此处具有开始/结束值:

<span id="col_fr" runat="server" enableviewstate="true">?</span>
<span id="col_to" runat="server" enableviewstate="true">?</span>

Then, when user click a linkbutton I want to use these values to write a text in a textbox on the modal popup that shows. 然后,当用户单击链接按钮时,我想使用这些值在显示的模式弹出窗口上的文本框中写入文本。 As I said, I can't make it work, anything I tried the result is that the values are lost when popup shows, even if I assign values to global variables before showing the popup. 就像我说的那样,我无法使其工作,我尝试的结果是,即使显示弹出窗口之前将值分配给全局变量显示弹出窗口时值也会丢失。 This is my linkbutton: 这是我的链接按钮:

<asp:LinkButton runat="server" ID="lnkAddDip"  OnClick="lnkAddDip_Click">

The idea behind the old question was to pass the values to the url as parameters and then in the page load use them, but then the table selection doesn't work anymore because at every selection the page refresh because of the url change. 老问题背后的想法是将值作为参数传递给url,然后在页面加载中使用它们,但是随后表选择不再起作用,因为在每次选择时,页面都会由于url的变化而刷新。 Please anyone, I'm totally lost and not for lack of trying! 请任何人,我完全迷失了,不是因为缺乏尝试!

OLD QUESTION asp.net pass a control value as parameter in onclientclick 老问题 asp.net在onclientclick中将控制值作为参数传递

I found similar questions but no one answer to my problem (or at least, I can't make anything working). 我发现了类似的问题,但没有人回答我的问题(或者至少我什么也做不了)。 I want to concatenate to the url of the active page some parameters like Home.aspx?col_fr=2 where instead of the fixed "2" I want to pass the value of a hidden field. 我想将诸如Home.aspx?col_fr = 2之类的一些参数连接到活动页面的URL,在这里我要传递一个隐藏字段的值,而不是固定的“ 2”。 How can I achive that? 我该如何实现?

This is my current code: 这是我当前的代码:

<asp:hiddenfield runat="server" id="hdnColonnaDa" EnableViewState="true"  />
<asp:LinkButton runat="server" ID="lnkAddDip"  OnClick="lnkAddDip_Click" OnClientClick="window.location='Home.aspx?col_fr=2';return false;">

Thanks 谢谢

It just have to move the parameter code oa javascript function like 它只需要将参数代码移至javascript函数之类的

function getURL(){
   var param1 = someField.value/*get field value*/;
   var url= "Home.aspx?col_fr="+ param1;
   window.location= url;
  return false;
}

Html HTML

<asp:LinkButton runat="server" ID="lnkAddDip"  OnClick="lnkAddDip_Click" OnClientClick="getURL()">

Don't use UpdatePanel or Modal popup server side. 不要使用UpdatePanel或Modal弹出服务器端。 You can use a Thickbox or similar jquery plugin to open the popoup. 您可以使用Thickbox或类似的jQuery插件打开弹出窗口。

Popup can be an inpage div or another page. 弹出窗口可以是页内div或其他页面。 In case of page, you can easily pass parameters in the url, in case of inpage div, you can get hidden field values. 如果是page,则可以轻松地在url中传递参数;如果是inpage div,则可以获取隐藏字段值。

You can find jquery overlay as Thickbox: just add css and js to your site, the right class on the link and fix the url to open. 您可以找到jQuery覆盖层作为Thickbox:只需将css和js添加到您的站点中,在链接上添加正确的类并修复URL以将其打开。

Solution using another page 使用另一页的解决方案

Imagine to use Colorbox ( http://www.jacklmoore.com/colorbox/ ): 想象一下使用Colorbox( http://www.jacklmoore.com/colorbox/ ):

PRE

Suppose you have your variable values in some hidden field in your page 假设您的变量值位于页面的某些隐藏字段中

TODO 去做

  1. include jquery in your page 在页面中包含jQuery
  2. Download and include css and js for colorbox 下载并包含用于colorbox的CSS和JS
  3. In your page add a html tag with on click event 在您的页面中添加带有点击事件的html标记
  4. Add a script section in your page 在页面中添加脚本部分

     function openLink() { var hiddenValue= $("#col_fr").text(); // or .html() var urlToOpen = "yourpage.aspx?ids=" + hiddenValue; $.colorbox({ iframe: true, width: "75%", height: "75%", href: urlToOpen }); } 

Then in Yourpage.aspx you can use url parameter "ids". 然后,在Yourpage.aspx中,可以使用url参数“ ids”。

Code is not tested! 代码未经测试!

Finally I did what I want by tuning the original javascript function. 最后,我通过调整原始javascript函数完成了我想要的工作。 I added 3 hiddenfield on the page and then I add this bit 我在页面上添加了3个hiddenfield,然后添加了此位

var hdncol_fr = document.getElementById("hdncol_fr").value;
var hdncol_to = document.getElementById("hdncol_to").value;
var hdnrow = document.getElementById("hdnrow").value;
if (hdncol_fr = '?'){
   document.getElementById("hdncol_fr").value = col - 1;
   document.getElementById("hdncol_to").value = col2 - 1;
   document.getElementById("hdnrow").value = row;
}

This way the hidden fields values are set only when user actively select some cells, when there is a postback event the javascript function returns '?' 这样,仅当用户主动选择某些单元格时,才设置隐藏字段的值;当发生回发事件时,javascript函数将返回“?”。 for the 3 values, so with the added code the hidden field maintains the previous values until user select something else. 对于这3个值,因此使用添加的代码,隐藏字段将保持先前的值,直到用户选择其他内容为止。

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

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