简体   繁体   English

一起实现JavaScript和ASP.NET交互

[英]Getting JavaScript and ASP.NET Interaction Together

Here's the definition of my ASP.NET GridView control: 这是我的ASP.NET GridView控件的定义:

<asp:GridView ID="gvReq" runat="server" AllowSorting="True" DataKeyNames="Req_ID,Approved,supervisor_login,Revised,Requested_Login,HasDocs" DataSourceID="objdsReq" AllowPaging="True" PageSize="30" >

In this GridView, I have the following template field: 在此GridView中,我具有以下模板字段:

<asp:TemplateField>
  <ItemStyle HorizontalAlign="Center" Width="50px"></ItemStyle>
  <ItemTemplate>
    <asp:Button ID="btnDelete" runat="server" CommandArgument='<%# Container.DataItemIndex %>' ForeColor="Red" CommandName="DeleteReq" OnClientClick="showConfirm(this); return false;" Text="Delete" />
  </ItemTemplate>
  <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

That OnClientClick="showConfirm(this); return false;" OnClientClick="showConfirm(this); return false;" method is shown below. 方法如下所示。 I don't claim to understand all of it because I got it from another post of mine ( ASP.NET GridView ItemTemplate LinkButton Support for RowCommand AFTER JavaScript Confirmation ). 我并没有声称理解所有这些内容,因为我是从我的另一篇文章中获得的( ASP.NET GridView ItemTemplate LinkBut​​ton对RowCommand在JavaScript确认后的支持 )。

<script type="text/javascript">
  var _source; // keeps track of the delete button for the row that is going to be removed
  var _popup; // keep track of the popup div
  function showConfirm(source) {
    this._source = source;
    this._popup = $find('mdlPopup');
    this._popup.show(); //  find the confirm ModalPopup and show it    
  }
  function okClick() {
    this._popup.hide(); // find the confirm ModalPopup and hide it    
    __doPostBack(this._source.name, ''); // use the cached button as the postback source
  }
  function cancelClick() {
    this._popup.hide(); // find the confirm ModalPopup and hide it 
    this._source = null; // clear the event source
    this._popup = null;
  }
</script>

I did understand enough about that JavaScript that I found online to correctly include these two HTML tags to get it all to work: 我确实了解到我在网上找到的JavaScript,足以正确地包含这两个HTML标签以使其全部正常工作:

<div id="popupDiv" runat="server" align="center" class="confirm" style="display: none">
  <img align="absmiddle" src="Images/important.png" /><b>CONFIRM:</b> Delete this item?<br />
  <asp:Button ID="btnOk" runat="server" Text="Yes" Width="50px" />
  <asp:Button ID="btnNo" runat="server" Text="No" Width="50px" />
</div>
<ajaxToolkit:ModalPopupExtender BehaviorID="mdlPopup" runat="server" TargetControlID="popupDiv" PopupControlID="popupDiv" OkControlID="btnOk" OnOkScript="okClick();" CancelControlID="btnNo" OnCancelScript="cancelClick();" BackgroundCssClass="modalBackground" />

Now that I have that all working, I realize the end result is still lacking. 现在我已经完成了所有工作,我意识到最终的结果仍然缺乏。

All that displays in my pop-up window is a vague confirmation dialog. 我的弹出窗口中显示的只是一个模糊的确认对话框。

屏幕截图

I was clicking to delete a row entry, but I mistakenly clicked the wrong row! 我正在单击以删除行条目,但是我错误地单击了错误的行!

I did not know this, so I proceeded to click OK to confirm, then noticed the wrong row getting deleted as I stepped through my code. 我不知道这一点,所以我继续单击“ OK进行确认,然后注意到在我单步执行代码时删除了错误的行。

I started looking at how to include additional data in my dialog box, which led me to the article c #, asp.net interaction with js with this little bit of code: 我开始研究如何在对话框中包含其他数据,这使我想到了这篇文章c#,asp.net与js的交互以及以下代码:

ASP.NET binding c #: 
<% # Bind ("column name")%> 
<% # Eval ("column name")%> 

The <a href/> binding c #: 
<A href = '<% # Eval ("column name ... aspx? Key = value & key = {0}")%>' /> 

The OnClientClick embedded js function (c # parameter is not passed): 
OnClientClick = "js function (parameter)" 

The OnClientClick embedded js function (pass a c # parameters): 
The OnClientClick = '<% # Eval ("column name", "js function ({0})")%>'> 
OnClientClick = '<% # Eval ("column name", "javascript: js function (\" {0} \ ")")%>'> 

The OnClientClick embedded js function (passing two or more c # parameters): 
OnClientClick = '<% # String.Format ("js function ({0}, {1}", Eval ("column name"), Eval ("column name"))%>' 

c # embedded js function: 
Response.Write ("<script> js built-in function ('parameter') </ script>"); 
Page.ClientScript.RegisterStartupScript (this.GetType (), "", "js-defined functions ('parameter')", true); 

js call back method: 
var result = "<% = method name (shape parameter)%>"; 
public method type the name of the method (parameter) {..}

(I pasted it here, just in case that site disappears) (我将其粘贴在这里,以防万一该网站消失了)

So, it seems like I should be able to send some more descriptive information to my Confirmation Dialog Box, like the row's ID or something. 因此,似乎我应该能够将更多描述性信息发送到“确认对话框”,例如行的ID或其他内容。

Is there a way, using the code I have shown, that someone can show me how to pass more data to my Confirmation Dialog Box? 使用我显示的代码,有没有办法使某人可以告诉我如何将更多数据传递到“确认对话框”?

Should I extract and send the String information to my Dialog Box or should/could the JavaScript use the source parameter to access the information I need? 我应该提取字符串信息并将其发送到对话框,还是JavaScript应该/可以使用source参数来访问所需的信息?

In that GridView control, 在该GridView控件中,

<asp:BoundField DataField="Req_ID" HeaderText="ID" SortExpression="Req_ID" />
<asp:BoundField DataField="Vendor_ID" HeaderText="Ven ID" SortExpression="Vendor_ID" />
<asp:BoundField DataField="Vendor_Name" HeaderText="Vendor" SortExpression="Vendor_Name" />

UPDATE: 更新:

Hi Dave! 嗨,戴夫!

In the View Source, the ID column has this header: 在“查看源代码”中,“ ID”列具有以下标题:

<th scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$gvReq','Sort$Req_ID')">ID</a></th>

The first table row has ID=70701 , and value 70701 is in two other table detail cells of that row: 表格的第一行具有ID = 70701 ,值70701位于该行的其他两个表格详细信息单元格中:

<tr onmouseover="this.style.backgroundColor='LightGray'" onmouseout="this.style.backgroundColor='White'">
  <td style="color:Blue;">
    <a onclick="javascript:popup_window=window.open('RequisitionDetails.aspx?Req_id=70701','ViewReqDetails','width=950,height=610,top=75,left=100,status=no, resizable= no, scrollbars=yes, toolbar= no,location= no, menubar=no,titlebar=no');popup_window.focus();" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$gvReq','Select$0')" style="color:Blue;">Details</a>
  </td>
  <td>70701</td>
  <td>206101</td>
  <td>EBM PAPST, INC</td>
  <td>6/26/2013</td>
  <td>58045 - HOT PO</td>
  <td>REPAIRS / db</td>
  <td align="center" style="color:Blue;width:50px;">
    <span id="ctl00_ContentPlaceHolder1_gvReq_ctl02_lblApprove" style="color:Black;">Approved</span>
  </td>
  <td align="center" style="color:Blue;width:50px;">
    <a onclick="javascript:popup_window=window.open('ReqReport.aspx?Req_id=70701','ViewReqDetails','width=810,height=620,top=75,left=75,status=no, resizable= no, scrollbars=no, toolbar= no,location= no, menubar=no,titlebar=no');popup_window.focus();" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$gvReq','Select$0')" style="color:Blue;">Print</a>
  </td>
  <td align="center" style="color:Blue;width:50px;">
    <a onclick="javascript:popup_window=window.open('ReqDocs.aspx?Req_id=70701','ViewReqDetails','width=550,height=500,top=75,left=75,status=no, resizable= no, scrollbars=yes, toolbar= no,location= no, menubar=no,titlebar=no');popup_window.focus();" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$gvReq','Select$0')" style="color:Blue;">Docs</a>
  </td>
  <td align="center" style="width:50px;">
    <input type="submit" name="ctl00$ContentPlaceHolder1$gvReq$ctl02$btnDelete" value="Delete" onclick="showConfirm(this); return false;" id="ctl00_ContentPlaceHolder1_gvReq_ctl02_btnDelete" style="color:Red;" />
  </td>
</tr>

So, the ID value I want is just a <td> field. 因此,我想要的ID值只是一个<td>字段。

How would I edit this to include that value? 我将如何编辑它以包含该值?

OnClientClick="showConfirm(this); return false;" OnClientClick =“ showConfirm(this);返回false;”

function showConfirm(source) {
    ...
  }

You can change the signature of this method to take additional parameters, eg itemName, and you could modify the js to alter the text that the popup is displaying (you would need to change your html a little, but no biggie). 您可以更改此方法的签名以采用其他参数,例如itemName,并且可以修改js来更改弹出窗口显示的文本(您需要稍微更改html,但不要大意)。 Have you inspected the source to see what it contains? 您是否检查了源以查看其内容? It might already have the information you want to display. 它可能已经具有您要显示的信息。

EDIT 编辑

Played with this, and have a quick way to get this done. 玩过这个,并有一个快速的方法来完成。 I'm using jquery. 我正在使用jquery。

  1. Change the popup html to be like so or similarm where the span is going to contain the additional data: 更改弹出HTML使其类似或类似,其中跨度将包含其他数据:


    <div id="popupDiv" runat="server" align="center" class="confirm" style="display: none; background-color: white">
      <img align="absmiddle" src="Images/important.png" /><b>CONFIRM:</b> Delete <span id="popupDescription"></span>?<br />
      <asp:Button ID="btnOk" runat="server" Text="Yes" Width="50px" />
      <asp:Button ID="btnNo" runat="server" Text="No" Width="50px" />
    </div>

  1. Update the button in your grid with a data=itemName="" attribute like below. 使用data = itemName =“”属性更新网格中的按钮,如下所示。 Note my data class id called 'DataClass', and has a property called 'Name'. 请注意我的数据类ID为“ DataClass”,并具有一个名为“ Name”的属性。 You can change this to match your data: 您可以更改它以匹配您的数据:


    <asp:Button ID="btnDelete" runat="server" CommandArgument='<%# Container.DataItemIndex %>' ForeColor="Red" CommandName="DeleteReq" data-itemName="<%# ((DataClass)Container.DataItem).Name %>" OnClientClick="showConfirm(this); return false;" Text="Delete" />

  1. Finally, change your function to inject this value into the popup: 最后,更改您的函数以将该值注入到弹出窗口中:


    function showConfirm(source) {
        //I use jquery to get the data-itemName attribute value and set it on the span
        var sourceElement = $(source);
        var additionalInfo = sourceElement.attr('data-itemName');
        var descriptionElement = $('#popupDescription');
        descriptionElement.text(additionalInfo);
        this._source = source;
        this._popup = $find('mdlPopup');
        this._popup.show(); //  find the confirm ModalPopup and show it    
    }

Granted, this is far from an elegant solution, but it works. 诚然,这远不是一个优雅的解决方案,但是它可以工作。

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

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