Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have this GridView:

<asp:GridView DataKeyNames="IdActivitate" ID="gvActivitati" runat="server"
                AutoGenerateColumns="false" CssClass="gvActivitati" HeaderStyle-CssClass="headerGrid" RowStyle-CssClass="rowsGrid">
<Columns>  
<pre><asp:BoundField DataField="Titlu" HeaderText="Activitate" />
                    <asp:BoundField DataField="IdActivity" HeaderText="ID" ReadOnly="true" />
                    <asp:BoundField DataField="Activity" HeaderText="Activity" />
<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="btnpopup" CssClass="popUp" runat="server" Text="LoadBugs">         </asp:Button>
  </ItemTemplate>
</asp:TemplateField>
</Columns>

On button click it opens a dialog in which i show a table with some information from database. This is my script:
$(document).ready(function () {
    $(".popUp").click(function () {
        $("#divModal").dialog({
            dialogClass: "no-close",
            modal: true,
            title: "Bug",
            width: 450,
            height: 440,
            buttons:{
                Close: function () {
                    $(this).dialog('close');
                }
            },
            open: LoadData()
        });
        $("#bug").empty();
        return false;
    });
});
function LoadData() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Default.aspx/GetBugs",
        data: "{}",
        dataType: "json",
        success: function (data) {
            var table = $("#tblBugs");
            for (var i = 0; i < data.d.length; i++) {
                table.append("<tr><td>" + data.d[i].IdBug + "</td><td>" + data.d[i].Bug + "</td><td>" + data.d[i].IdActivity+ "</td></tr>");
            }
        },
        error: function (result) {
            alert("fail");
        }
    });
}

And i call the ajax through a WebMethod:
public class BugDetails
   {
       public string IdBug { get; set; }
       public string Bug { get; set; }
       public string IdActivity { get; set; }

   }
   [WebMethod]
   public static BugDetails[] GetBugs()
   {
       DataTable dt = new DataTable();
       List<BugDetails> bugs = new List<BugDetails>();
       using (MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
       {
           using (MySqlCommand cmd = new MySqlCommand("select * from bugs" , conn)) // here in the where clause i should somehow add the Id from the row i clicked on in the gridview
           {
               conn.Open();
               MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
               adp.Fill(dt);
               conn.Close();
               foreach(DataRow dtrow in dt.Rows)
               {
                   BugDetails bug = new BugDetails();
                   bug.IdBug = dtrow["IdBug"].ToString();
                   bug.Bug = dtrow["Bug"].ToString();
                   bug.Status = dtrow["IdActivity"].ToString();
                   bugs.Add(bug);
               }
           }
       }
       return bugs.ToArray();
   }


So to better explain myself i have the two tables, the Activities table who populates the grid, and the Bugs Table who populates the dialog. What i want is when i click the button to show information only for the activity in that row. Currently it shows all the information. Can you help? Thank you

What I have tried:

i tried to do it without a webmethod but it doesn't work or i don't know how to make it work otherwise :)
Posted
Updated 26-Jan-17 21:55pm
v3

Add the key as a data- attribute on your button:
<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="btnpopup" runat="server" 
        Text="LoadBugs" CssClass="popUp"
        data-activity-id='<%# Eval("IdActivity") %>' />
  </ItemTemplate>
</asp:TemplateField>

You can then retrieve the ID from your Javascript, and pass it to the server:
$(document).ready(function () {
    $(".popUp").click(function () {
        var activityId = $(this).data("activityId");
        
        $("#bug").empty();
        
        $("#divModal").dialog({
            ...
            open: LoadData(activityId)
        });
        
        return false;
    });
});

function LoadData(activityId) {
    $.ajax({
        ...
        data: JSON.stringify({ activityId: activityId }),
        ...
    });
}

And then use the ID on the server to filter the list:
[WebMethod]
public static BugDetails[] GetBugs(int activityId)
{
    using (MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
    using (MySqlCommand cmd = new MySqlCommand("select * from bugs where IdActivity = @ActivityId" , conn))
    { 
        cmd.Parameters.AddWithValue("@ActivityId", activityId);
        
        DataTable dt = new DataTable();
        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        adp.Fill(dt);
        
        List<BugDetails> bugs = new List<BugDetails>(dt.Rows.Count);
        
        foreach(DataRow dtrow in dt.Rows)
        {
            bugs.Add(new BugDetails
            {
                IdBug = dtrow["IdBug"].ToString(),
                Bug = dtrow["Bug"].ToString(),
                Status = dtrow["Status"].ToString(),
            });
        }
        
        return bugs.ToArray();
    }
}
 
Share this answer
 
Comments
Member 12869977 27-Jan-17 2:58am    
Thank you for the answer!! It worked :)
//Put an hidden field conntaining the KEY value
<ItemTemplate>
  <asp:HiddenField ID="id_name" class="hChanged" runat="server" Value='<%# Eval("IdActivity") %>' />
    <asp:Button ID="btnpopup" CssClass="popUp" runat="server" Text="LoadBugs">     </asp:Button>
</ItemTemplate>


//Access the KEY from the button click event in jQuery click event
$(this).siblings(".hChanged").val()
 
Share this answer
 
v2
Comments
Richard Deeming 26-Jan-17 12:39pm    
Except .closest only finds ancestors of the given element. The hidden input is a sibling, so it won't be found.
.closest() | jQuery API Documentation[^]

You've also forgotten to set the value of the hidden input, so it will always have the value 0.
sonymon mishra 26-Jan-17 23:25pm    
Thanks for citing. I was posting on go. Tried to share the idea, the code might not be a plug and play. Good Day

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900