在GridView中单击按钮的JS代码

时间:2014-04-16 07:57:52

标签: c# asp.net gridview

我有一个GridView控件,里面我有一个文件上传控件,上面有一个按钮来上传文件,每个FileControl都有自己的上传按钮,我使用RowCommand Event.Now上传文件我必须显示进度条文件上传控件,我在这个链接的第3个答案Link中实现了,并在JS中进行了一些修改

我面临的问题是,如果我点击GridView的第二行上传按钮,它只能验证Row的控件。

这是我的网格视图

<asp:GridView ID="Grid1" runat="server" AutoGenerateColumns="false" OnRowDataBound="Grid1_RowDataBound"
                                    OnRowCommand="Grid1_RowCommand" OnRowDeleting="Grid1_RowDeleting"
                                    CssClass="table">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Files" ItemStyle-HorizontalAlign="Left" HeaderStyle-CssClass="GridViewHeader">
                                            <ItemTemplate>
                                             <asp:FileUpload ID="File1" runat="server" Width="98%" CssClass="filestat" />
                                               <asp:Button ID="btnuploadfiles" runat="server" CommandName="upload" Text="Upload" OnClientClick="return ProgressBar('File1')" />
                                             </ItemTemplate>
                                        </asp:TemplateField>
                                         </Columns>
                                </asp:GridView>

和我的JS代码

function ProgressBar(Id) {
            var fileControl = GetClientID(Id).attr("id");
            if (document.getElementById(fileControl).value != "") {
                $("#divUpload").fadeIn("slow");
                $('#popup_box').fadeIn("slow");
                document.getElementById("divUpload").style.height = document.body.clientHeight + 'px';
                id = setInterval("progress()", 20);
                return true;
            }
            else {
                alert("Select a file to upload");
                return false;
            }

        }
//This function returns me ClientID of the server control       
        function GetClientID(id, context) {
            var el = $("#" + id, context);
            if (el.length < 1)
                el = $("[id$=_" + id + "]", context);
            return el;
        }

我也尝试过从RowDataBound设置OnClientClick,但它仍然只验证第一行。

                               protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    var btnupd = (Button)e.Row.FindControl("btnuploadfiles");

        btnupd.OnClientClick = "return ProgressBar('File1')";
    }
    }

1 个答案:

答案 0 :(得分:1)

尝试以下内容;

$("#<%=Grid1.ClientID%> tr").click(function(){
   alert("Row clicked");
});

我认为使用ClientID功能会更适合您。单击tr后,您应该能够读取实际tds数组中的单元格。

尝试添加此

protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var button = (Button)e.Row.FindControl("btnuploadfiles");
        var fileUpload = (FileUpload)e.Row.FindControl("File1");

        string className = "class_" + new Random().Next();

        button.Attributes.Add("class", "rowClick");
        fileUpload.Attributes.Add("class", "rowClick"); 

        button.Attributes.Add("customClass", className);
        button.Attributes.Add("customClass", className); 
    }
}

对于脚本添加:

<script>
  $(document).ready(function () {
      $(".rowClick").click(function () {
          var customClass = this.attr("customClass"); 
          alert("Now the current row you selected has this as a common class : " + customClass);
      });
  }); 
</script>