避免在网格视图中进行自动postback
本文关键字:postback 网格 视图 | 更新日期: 2023-09-27 18:25:12
嘿,朋友们,我想避免点击图像按钮回发,这是我的代码:-
它有一个带有图像按钮的网格视图,用于编辑所选行
<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的row命令:-
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();
}
如果我正确阅读了您的问题,您可能想尝试将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>
我认为最好的情况是将你的网格视图放在更新面板中,并使整个过程部分返回,因为你需要图像按钮才能转到服务器,因为你说图像按钮用于编辑所选行。