它显示相同的信息

它显示相同的信息

本文介绍了为什么当我使用选择gv_RowCommand时,它显示相同的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This is my gv_RowCommand code behind. The problem is when I click button "cmdSelect" in different row,it will display the same information. All my data, the status is '2'. Updated code:







Protected Sub gvCheckList_RowCommand(sender As Object, e As GridViewCommandEventArgs)
     Try
        If e.CommandName = "cmdSelect" Then
            Dim index As Integer = Int32.Parse(e.CommandArgument.ToString())
            Dim row As GridViewRow = gvCheckList.Rows(index)
            Dim MatricNo As String = gvCheckList.DataKeys(index).Value

            sql = "SELECT a.MatricNo, b.Name, b.Faculty, a.Date, a.KaunselingID, b.TelNo, " _
                & "c.Officer " _
                & "FROM KaunselingPL AS a " _
                & "INNER JOIN DbStudent..Personal AS b ON a.MatricNo = b.MatricNo " _
                & "INNER JOIN KaunselingDT AS c ON a.KaunselingID = c.KaunselingID " _
                & "WHERE (a.Status = '2' AND a.MatricNo = '" & MatricNo &"')"

            dbconn = New OleDbConnection(strCon)
            dbconn.Open()

            dbcomm = New OleDbCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
            dbread.Read()
            If (dbread.HasRows()) Then
               Me.lblName.Text = dbread("Name")
               Me.lblMatricNo.Text = dbread("MatricNo")
               Me.lblFaculty.Text = dbread("Faculty")
               Me.lblTelNo.Text = dbread("TelNo")
               Me.lblDate.Text = CDate(dbread("Date")).ToString("dd/MM/yyyy")
               Me.lblOfficer.Text = dbread("Officer")
            End If
            dbread.Close()
            dbcomm = Nothing
            dbconn.Close()
        End If
        Catch ex As Exception
            Response.Write(ex.Message.ToString())
     End Try
    End Sub







<asp:gridView id="gvCheckList"  CssClass="gridViewTable" autogeneratecolumns="False" allowpaging="True"  Width="100%"  EmptyDataText="No Recordd" runat="server" PageSize="20" OnRowCommand="gvCheckList_RowCommand">
                               
                        <FooterStyle CssClass="gridViewFooter" />
                        <HeaderStyle CssClass="gridViewHeader" />
                        <AlternatingRowStyle CssClass="gridViewAlternating" />

                        <Columns> 
                            <asp:BoundField DataField="ROWNUMBER" HeaderText="No." HeaderStyle-Width="2%"></asp:BoundField>
                            <asp:ButtonField DataTextField="MatricNo" CommandName="cmdSelect" HeaderText="Matric No" meta:resourcekey="ButtonFieldResource1"  HeaderStyle-Width="15%" ></asp:Butt>
                            <asp:ButtonField DataTextField="Name" HeaderText="Name" HeaderStyle-Width="32%"></asp:Butt>
                            <asp:BoundField DataField="Faculty" HeaderText="Fakulti" HeaderStyle-Width="25%"></asp:BoundField>
                            <asp:BoundField DataField="Date" HeaderText="Date" HeaderStyle-Width="25%" DataFormatString="{0:dd/MM/yyyy}"></asp:BoundField> 

                            <asp:BoundField DataField="KaunselingID" HeaderText="Hidden">
                                <ItemStyle CssClass="hidden" VerticalAlign="Top" />
                                <HeaderStyle CssClass="hidden" />
                                <FooterStyle CssClass="hidden" />
                            </asp:BoundField> 
                        </Columns>

                        <FooterStyle CssClass="gridViewFooter" />
                        <HeaderStyle ForeColor="White" CssClass="gridViewHeader" />
                        
                        <PagerSettings FirstPageText="<<" LastPageText=">>" Mode="NextPreviousFirstLast" NextPageText=">" PreviousPageText="<" />
                   </asp:gridView>

推荐答案

Protected Sub gvCheckList_RowCommand(sender As Object, e As GridViewCommandEventArgs)
	Try
		If e.CommandName = "cmdSelect" Then
			Dim index As Integer = Int32.Parse(e.CommandArgument.ToString())
			Dim row As GridViewRow = gvCheckList.Rows(index)
Dim matricNo As String = row.Cells(1).Text.ToString()
Response.Write("Matric No = "&matricNo)
			sql = "SELECT a.MatricNo, b.Name, b.Faculty, a.Date, a.KaunselingID, b.TelNo, " _
			& "c.Officer " _
			& "FROM KaunselingPL AS a " _
			& "INNER JOIN DbStudent..Personal AS b ON a.MatricNo = b.MatricNo " _
			& "INNER JOIN KaunselingDT AS c ON a.KaunselingID = c.KaunselingID " _
			& "WHERE (a.Status = '2' and a.MatricNo='"& matricNo &"')"
Response.Write("Sql Query = "&sql)		 
			dbconn = New OleDbConnection(strCon)
			dbconn.Open()
		 
			dbcomm = New OleDbCommand(sql, dbconn)
			dbread = dbcomm.ExecuteReader()
			dbread.Read()
			If (dbread.HasRows()) then 
				Me.lblName.Text = dbread("Name")
				Me.lblMatricNo.Text = dbread("MatricNo")
				Me.lblFaculty.Text = dbread("Faculty")
				Me.lblTelNo.Text = dbread("TelNo")
				Me.lblDate.Text = CDate(dbread("Date")).ToString("dd/MM/yyyy")
				Me.lblOfficer.Text = dbread("Officer")
			End If 
			dbread.Close()
			dbcomm = Nothing
			dbconn.Close()
		End If
	Catch ex As Exception
		Response.Write(ex.Message.ToString());
	End Try
End Sub


If e.CommandName = "cmdSelect" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = gvCheckList.Rows(index)

            Me.hidIDKaunseling.Value = Convert.ToString(row.Cells(5).Text)
            Me.lblMatricNo.Text = Convert.ToString(row.Cells(6).Text)

            '---LOAD SENARAI---
            dbconn = New OleDb.OleDbConnection(strCon)
            dbconn.Open()

            sql = "SELECT a.MatricNo, b.Name, b.Faculty, a.Date, a.KaunselingID, b.TelNo, " _
                & "c.Officer " _
                & "FROM KaunselingPL AS a " _
                & "INNER JOIN DbStudent..Personal AS b ON a.MatricNo = b.MatricNo " _
                & "INNER JOIN KaunselingDT AS c ON a.KaunselingID = c.KaunselingID " _
                & "WHERE (a.Status = '2' and a.MatricNo='" & Me.lblMatricNo.Text & "'"

            dbcomm = New OleDbCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
            dbread.Read()

            Me.lblName.Text = dbread("Name")
            Me.lblMatricNo.Text = dbread("MatricNo")
            Me.lblFaculty.Text = dbread("Faculty")
            Me.lblTelNo.Text = dbread("TelNo")
            Me.lblDate.Text = CDate(dbread("Date")).ToString("dd/MM/yyyy")
            Me.lblOfficer.Text = dbread("Officer")

            dbread.Close()
            dbcomm = Nothing
            dbconn.Close()
        End If







<asp:gridview id="gvCheckList" cssclass="gridViewTable" autogeneratecolumns="False" allowpaging="True" width="100%" emptydatatext="No Record" runat="server" pagesize="20" onrowcommand="gvCheckList_RowCommand" xmlns:asp="#unknown">
                               
                        <footerstyle cssclass="gridViewFooter" />
                        <HeaderStyle CssClass="gridViewHeader" />
                        <alternatingrowstyle cssclass="gridViewAlternating" />

                        <columns> 
                            <asp:boundfield datafield="ROWNUMBER" headertext="No." headerstyle-width="2%"></asp:boundfield>
                            <asp:buttonfield datatextfield="MatricNo" commandname="cmdSelect" headertext="Matric No." meta:resourcekey="ButtonFieldResource1" headerstyle-width="15%" xmlns:meta="#unknown"></asp:buttonfield>
                            <asp:boundfield datafield="Name" headertext="Student's Name" headerstyle-width="35%"></asp:boundfield>
                            <asp:boundfield datafield="Faculty" headertext="Fakulti" headerstyle-width="25%"></asp:boundfield>
                            <asp:boundfield datafield="Date" headertext="Date" headerstyle-width="25%" dataformatstring="{0:dd/MM/yyyy}"></asp:boundfield> 

                            <asp:boundfield datafield="KaunselingID" headertext="Hidden">
                                <itemstyle cssclass="hidden" verticalalign="Top" />
                                <HeaderStyle CssClass="hidden" />
                                <footerstyle cssclass="hidden" />
                            </asp:boundfield>
                           
                            <asp:boundfield datafield="Matric No." headertext="Hidden">
                                <itemstyle cssclass="hidden" verticalalign="Top" />
                                <HeaderStyle CssClass="hidden" />
                                <footerstyle cssclass="hidden" />
                            </asp:boundfield>
                             
                        </columns>

                        <footerstyle cssclass="gridViewFooter" />
                        <HeaderStyle ForeColor="White" CssClass="gridViewHeader" />
                        <pagersettings firstpagetext="<<" lastpagetext=">>" mode="NextPreviousFirstLast" nextpagetext=">" previouspagetext="<" />

                   </asp:gridview>


这篇关于为什么当我使用选择gv_RowCommand时,它显示相同的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:17