请解决以下问题 [英] please solve the following Issue

查看:72
本文介绍了请解决以下问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



在我的代码中,我使用了一个arraylist来存储从数据库中检索到的用户名。我有角色gridview。它包含多个角色名称和图像用户按钮。单击图像按钮时,应显示与其关联的相应用户名。在一个角色中有许多用户关联。但我的代码只显示一个用户。与图像相关联的所有用户如何显示在图像按钮上请点击请帮助。

我的代码如下: -

  if (e.CommandName ==   ImageButton2_Click
{
int rowindex = Convert.ToInt32(e.CommandArgument);
// 获取行
GridViewRow gvr = GridView2.Rows [rowindex];
{
标签ID =(标签)GridView2.Rows [rowindex] .Cells [ 1 ]。FindControl( Role_ID);
string Role_ID = ID.Text;
con.Open();

使用(SqlCommand comm = new SqlCommand( 从登录中选择[UserName],其中Role_ID = @ Role,con))
{
// 2.定义命令对象中使用的参数
SqlParameter para = ;
para = new SqlParameter();
para.ParameterName = @ Role;
para.Value = Role_ID;
comm.Parameters.Add(para);
SqlDataReader reade = comm.ExecuteReader();
ImageButton button =
(ImageButton)GridView2.Rows [rowindex] .Cells [ 5 ]。FindControl( ImageButton2);
button.Visible = false ;

ArrayList yourList = new ArrayList();
if (reade.HasRows)
{
if (reade) != null
{
while (reade.Read())
{
// ImageButton button =(ImageButton)GridView2.Rows [rowindex] .Cells [5 ]。.FindControl(ImageButton2);
button.Visible = true ;

yourList.Add(Convert.ToString(reade [ 0 ]));
会话[ UserName] = yourList [ 0 ];
}
}
}
}
if (会话[ UserName]。ToString()== null
{
ImageButton button =
(ImageButton)GridView2.Rows [rowindex] .Cells [ 5 ]。FindControl( ImageButton2);
button.Visible = false ;
}
else
{
ImageButton button =
(ImageButton)GridView2.Rows [rowindex]。单元格[ 5 ]。FindControl( ImageButton2);
button.Visible = true ;

ClientScript.RegisterStartupScript( this .GetType(), myalert alert(' + Session [ UserName]。ToString()+ '); true );

con.Close();
}
}
}

解决方案

您需要自己开始查看代码:我们从这里做不了多少,因为我们无法在相同的条件下运行它。例如,我们无权访问您的数据库,因此我们甚至无法开始复制您的条件。



因此,请按照建议使用调试器,开始查看代码的作用以及它显示的数据。



例如,你说它返回一行 - 你怎么知道的?您看到了什么来决定?读者?客户端显示?

返回哪个用户?首先?最后?真正的用户?或者它是一个奇怪的名称,如System.Collections.ArrayList,这是我对你的代码的期望?



想想你在做什么,并使用调试器 - 因为这是你解决这个问题的唯一方法,我们不能为你做到这一点!

Hello Everyone,

In my code i have used an arraylist for storing my usernames which are retrieved from database. I have an role gridview. that contains several role name and image user button. when the imagebutton is clicked the corresponding usernames associate with it should display. In one role there are many users are associated. but my code displays only one user. How all the users associated with the role will be display on imagebutton click please help.
My code is as follows:-

if (e.CommandName == "ImageButton2_Click")
{
   int rowindex = Convert.ToInt32(e.CommandArgument);
   //Get Row
   GridViewRow gvr = GridView2.Rows[rowindex];
   {
      Label ID = (Label)GridView2.Rows[rowindex].Cells[1].FindControl("Role_ID");
      string Role_ID = ID.Text;
      con.Open();

      using (SqlCommand comm = new SqlCommand("select [UserName] from Login where Role_ID =@Role", con))
      {
         // 2. define parameters used in command object
         SqlParameter para = null;
         para = new SqlParameter();
         para.ParameterName = "@Role";
         para.Value = Role_ID;
         comm.Parameters.Add(para);
         SqlDataReader reade = comm.ExecuteReader();
         ImageButton button =
            (ImageButton)GridView2.Rows[rowindex].Cells[5].FindControl("ImageButton2");
         button.Visible = false;
                   
         ArrayList yourList = new ArrayList();
         if (reade.HasRows)
         {
            if (reade != null)
            {
               while (reade.Read())
               {
                  //ImageButton button =(ImageButton)GridView2.Rows[rowindex].Cells[5].FindControl("ImageButton2");
                  button.Visible = true;
                   
                  yourList.Add(Convert.ToString(reade[0]));
                  Session["UserName"] = yourList[0];
               }
            }
         }
      }
      if (Session["UserName"].ToString() == null)
      {
         ImageButton button =
            (ImageButton)GridView2.Rows[rowindex].Cells[5].FindControl("ImageButton2");
         button.Visible = false;
      }
      else
      {
         ImageButton button =
            (ImageButton)GridView2.Rows[rowindex].Cells[5].FindControl("ImageButton2");
         button.Visible = true;

         ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Session["UserName"].ToString() + "');", true);

         con.Close();
      }
   }
}

解决方案

You need to start looking at your code yourself: we can't do much from here because we can't run it under the same conditions you can. For example, we do not have access to your database, so we can't even begin to duplicate your conditions.

So, use the debugger as has been suggested and start looking at what the code does and what data it is showing.

For example, you say it "returns one row" - how do you know that? What did you look at to decide that? The reader? The client display?
Which user does it return? The first? The last? A real user? Or is it an "Odd" name, like "System.Collections.ArrayList" which is what I would expect from your code?

Think about what you are doing, and use the debugger - because that is the only way you are going to solve this, and we can't do it for you!


这篇关于请解决以下问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆