避免网格视图中的自动后备

时间:2012-02-21 07:28:40

标签: c# asp.net

嘿朋友们,我想避免回复点击图片按钮,这是我的代码: -

它有一个gridview,带有一个用于编辑所选行的图像按钮

<form id="form1" runat="server">
  <asp:Label ID="lblsearch" Text="Search by" runat="server"></asp:Label>
  <asp:DropDownList ID="ddlsearch" runat="server" OnSelectedIndexChanged="SearchProject" AutoPostBack="true">
    <asp:ListItem Text="Select" Value="select" Selected="True"></asp:ListItem>
  </asp:DropDownList>                        
  <asp:Button ID="btnclear" runat="server" Text="Clear" OnClick="btnclear_Click" />
  <asp:Label ID="lblsearchmsg" runat="server" ForeColor="#FF3300"></asp:Label>
  <br />
  <asp:GridView ID="gviewprojectallocation" runat="server" CellPadding="4"
       ForeColor="Black" GridLines="Vertical" EnableViewState="true"
       AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE"
       BorderStyle="None" BorderWidth="1px"
       onrowcommand="gviewprojectallocation_RowCommand"
       onrowcancelingedit="gviewprojectallocation_RowCancelingEdit" 
       onrowediting="gviewprojectallocation_RowEditing" 
       onrowupdating="gviewprojectallocation_RowUpdating">
       <AlternatingRowStyle BackColor="White" />
       <FooterStyle BackColor="#CCCC99" />
       <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
       <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
       <RowStyle BackColor="#F7F7DE" />
       <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
       <SortedAscendingCellStyle BackColor="#FBFBF2" />
       <SortedAscendingHeaderStyle BackColor="#848384" />
       <SortedDescendingCellStyle BackColor="#EAEAD3" />
       <SortedDescendingHeaderStyle BackColor="#575357" />
       <Columns>
         <asp:TemplateField>
           <ItemTemplate>
             <asp:ImageButton  CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' runat="server" id="ImageButton1" ImageUrl="~/images/edit.png" CommandName="Edit"/>
           </ItemTemplate>
         </asp:TemplateField>
         <asp:BoundField DataField="associate_id" ReadOnly="true" HeaderText="Associate ID"/>
         <asp:BoundField DataField="Associate_Name" ReadOnly="false" HeaderText="Associate Name" />
         <asp:BoundField DataField="involve_percent" ReadOnly="false" HeaderText="Involve %" />
       </Columns>
  </asp:GridView>
</form>
<br />
</center>

代码背后: -

//在页面加载时在下拉列表中加载信息

protected void Page_Load(object sender, EventArgs e)
{
  SqlConnection myconnection = new SqlConnection(constring);
  SqlCommand mycommand = new SqlCommand();
  mycommand.Connection = myconnection;
  int i = 1;

  SqlDataReader mydatareader = null;
  myconnection.Open();
  mycommand.CommandText = "select Project_Code,Project_Name from Project_Status_Report;";
  mycommand.CommandType = CommandType.Text;

  mydatareader = mycommand.ExecuteReader();
  if (!IsPostBack)
  {
    while (mydatareader.Read())
    {
      ddlsearch.Items.Add((string)mydatareader["Project_Name"]);
      ddlsearch.Items[i].Value = Convert.ToString(mydatareader["Project_Code"]);
      i++;
    }
  }
  myconnection.Close();
}

//用于根据下拉列表中选择的值进行搜索: -

protected void SearchProject(object sender, EventArgs e)
{
  try
  {
    SqlConnection myconnection = new SqlConnection(constring);
    SqlCommand mycommand = new SqlCommand();
    DataSet mydataset = new DataSet();
    SqlDataAdapter mydataadapter = new SqlDataAdapter();

    if (ddlsearch.SelectedValue == "select")
    {
      Response.Redirect("ProjectAllocation.aspx");
    }
    else
    {
      mycommand.CommandText = "select P.associate_id,T.Associate_Name,P.involve_percent from Associates_Info as T inner join Associate_Project as P on T.Associate_ID=P.associate_id where P.project_code = @procode;";
      mycommand.Parameters.Add("@procode", SqlDbType.Int);
      mycommand.Parameters["@procode"].Value = ddlsearch.SelectedValue;

      mycommand.CommandType = CommandType.Text;
      myconnection.Open();
      mycommand.Connection = myconnection;

      mydataadapter.SelectCommand = mycommand;

      mydataadapter.Fill(mydataset);

      if (mydataset == null || mydataset.Tables.Count == 0 || mydataset.Tables[0].Rows.Count == 0)
      {
        lblsearchmsg.Text = "Record not found";
      }

      gviewprojectallocation.DataSource = mydataset;
      gviewprojectallocation.DataBind();

      myconnection.Close();
    }
  }
  catch (Exception exp)
  {
     lblsearchmsg.Text = "Enter valid information";
  }
}

// gridview的行命令: -

 protected void gviewprojectallocation_RowCommand(object sender, GridViewCommandEventArgs e)
 {
   if (e.CommandName == "Edit")
   {
     int index = Convert.ToInt32(e.CommandArgument);
     GridViewRow row = gviewprojectallocation.Rows[index];
   }
 }

//网格视图行编辑事件

protected void gviewprojectallocation_RowEditing(object sender, GridViewEditEventArgs e)
{                        
  gviewprojectallocation.EditIndex = e.NewEditIndex;
  gviewprojectallocation.DataBind();
}

2 个答案:

答案 0 :(得分:0)

如果我正确地阅读了您的问题,您可能需要尝试将OnClientClick='return confirm_ambiguous_functionality();'添加到ImageButton的定义以及一些JavaScript中。或者你可以简单地让它不返回任何内容(例如,javascript:void(0);),具体取决于预期的功能。

<script type="text/javascript">
    function confirm_ambiguous_functionality() {
        return confirm("Are you sure you want to do whatever action you just clicked on?");
    }
</script>

答案 1 :(得分:0)

我认为最适合您情况的方案是将您的gridview放在更新面板中并使整个过程部分回发,因为您需要图像按钮才能转到服务器,因为您说的是图像按钮用于编辑所选行。