簡體   English   中英

如何從aspx的Gridview行中檢索主鍵以在userControl.ascx中使用?

[英]How can i retrieve the primary key from a Gridview row in aspx to use in a userControl.ascx?

我有一個GridView,其中包含來自mysql數據庫的數據綁定字段。 它包含兩列IdTask(主鍵),TaskName和一個打開jQuery對話框的按鈕。 我需要基於單擊按鈕的行中任務的ID來填充此對話框。 我有一個帶有另一個Gridview的用戶控件,其中包含我需要在對話框中顯示的所述數據。

這是我到目前為止所擁有的:

aspx頁面:

  <script>
    $(document).ready(function () {
        $(".modalFiles").click(function () {
            var id = $(this).data("activitateId");
            console.log(id);
            $("#divModalFiles").dialog({
                dialogClass: "no-close",
                modal: true,
                title: "Files",
                width: 450,
                height: 440,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                open: function (type, data) {
                    $(this).parent().appendTo("form");
                }

            });
            return false;
        });
    });
</script> 
    <form id="form1" runat="server" style="padding: 10px; width: 550px">
        <asp:GridView DataKeyNames="IdTask" ID="gvTask" runat="server"
            AutoGenerateColumns="false">
            <Columns>
                    <asp:BoundField DataField="IdTask" HeaderText="ID" />
                    <asp:BoundField DataField="Name" HeaderText="Task"  />
            <asp:TemplateField>
                            <ItemTemplate>
                                <asp:ImageButton ID="btnFile" CssClass="btnFiles" runat="server" ImageUrl="img/files.png.png"/>
                            </ItemTemplate>
                    </asp:TemplateField>
            </Columns>
            </asp:GridView>
        <div id="divModalFiles" style="display:none">
                <ucFiles:Files runat="server"/>
            </div>
    </form>
    </body>

ascx userControl:

<div id="file">
          <asp:Literal ID="litMesaj" runat="server" Visible="false"></asp:Literal>
          <asp:FileUpload ID="fUpload" runat="server" />

          <br />
          <br />
          <asp:Label ID="lblDescFile" runat="server" Text="Description"></asp:Label>
          <asp:TextBox ID="txtDescFile" runat="server" ></asp:TextBox>
          <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
          <br />
          <br />

          <asp:GridView ID="gvFiles" DataKeyNames="IdFiles" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded">
              <Columns>
                  <asp:BoundField DataField="IdFiles" HeaderText="ID" />
                  <asp:BoundField DataField="IdTask" HeaderText="IdTask" />
                  <asp:BoundField DataField="Name" HeaderText="File Name" />
                  <asp:BoundField DataField="Description" HeaderText="Description" />

                  <asp:TemplateField>
                      <ItemTemplate>
                          <asp:LinkButton ID="lnkDownload" runat="server" Text="Download File" OnClick="lnkDownload_Click" CommandArgument='<%# Eval("Url") %>'></asp:LinkButton>
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField>
                      <ItemTemplate>
                          <asp:ImageButton runat="server" ID="btnDelFile" ImageUrl="~/delete.png.png" OnClientClick="return confirm('Sure?');" OnClick="btnDelFile_Click"/>
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>              
          </asp:GridView>
      </div>  

ascx.cs userControl

 // the function i call in page load to display the files
 private void loadFiles()
    {
        string sqlFiles = "select * from files where IdTask=" + ?? ;// here is where i need the help how can i retrieve the IdTask from the gridview on the aspx Page? 
        MySqlCommand cmd = new MySqlCommand(sqlFiles, conn);

    MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
    DataTable dtFiles = new DataTable();
    adp.Fill(dtFiles);
        gvFiles.DataSource = dtFiles;
        gvFiles.DataBind();
    }  

你能幫助我嗎 ? 沒有該ID,我也無法使用下載或上傳文件功能。 當前僅顯示帶有文件上傳和上傳按鈕的表格
如果我沒有明確說明自己(英語不是我的母語),請隨時提出任何問題,我會解釋

您遇到的第一個問題是需要回發才能填充彈出窗口。 這意味着<iframe>中的彈出窗口將使用包含用戶.ascx控件的另一個獨立的.aspx頁。 您通過URL傳遞activitateId 注意: <iframe>不會回發,但會不刷新主頁的情況下運行popup.aspx頁面的頁面生命周期。

<div id="divModalFiles" style="display:none">
    <iframe id="iframe_id" runat=server src="popup.aspx" ... />
</div>

popup.aspx包含以下內容:

<html>
<head> 
    whatever you need here
</head>
<body>  
  <ucFiles:Files runat="server"/>
</body>
</html>

在JS / JQuery中,您可以在調用彈出窗口之前修改iframe src屬性:

function AlterIframeSrc( ID ) 
{
  $('#iframe_id').attr( 'src', 'popup.aspx?id=' + ID; );
}

選項2

或者,根據彈出窗口可能包含的數據量,您可以在主gridview的每一行中創建預加載的隱藏div。
與其他任何服務器控件一樣,將您的.ascx拖放到Main Gridview中,並將activitateId作為控件參數傳遞給它。

但是對於每個activitateId您只能獲取少量數據,否則這樣做會影響性能。 但是您不再需要回發來獲取數據。

這是最初性能下降的折衷方案。
好處是您可以使用簡單的JavaScript來連接彈出窗口。

<asp:TemplateField>
  <ItemTemplate>
     <asp:ImageButton runat="server" ID="btnDelFile" ImageUrl="~/delete.png.png" OnClientClick="return confirm('Sure?');" OnClick="btnDelFile_Click"/>
     <div id="divModalFiles" style="display:none">
       <ucFiles:Files runat="server" IDTask='<%# Eval("activitateId") %>'/>
     </div>
  </ItemTemplate>
</asp:TemplateField>

我添加的User Control屬性IDTask將在您的.ascx中定義為可綁定的公共屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM