本文将为您提供关于更新gridviewasp.net中的复选框更改数据库c#的详细介绍,我们还将为您解释aspnet的gridview里数据修改的相关知识,同时,我们还将为您提供关于.net–在gri
本文将为您提供关于更新gridview asp.net中的复选框更改数据库c#的详细介绍,我们还将为您解释aspnet的gridview里数据修改的相关知识,同时,我们还将为您提供关于.net – 在gridview末尾插入复选框列、android – 在gridview复选框中取消选中,同时向上和向下滚动gridview、asp.net gridview复选框选择、asp.net – 从标签容器中的数据绑定gridview丢失更新面板触发器的实用信息。
本文目录一览:- 更新gridview asp.net中的复选框更改数据库c#(aspnet的gridview里数据修改)
- .net – 在gridview末尾插入复选框列
- android – 在gridview复选框中取消选中,同时向上和向下滚动gridview
- asp.net gridview复选框选择
- asp.net – 从标签容器中的数据绑定gridview丢失更新面板触发器
更新gridview asp.net中的复选框更改数据库c#(aspnet的gridview里数据修改)
解决方法
假设您希望在Checked状态更改后立即更新(用户单击CheckBox),则必须先设置AutopostBack =“true”.
然后你可以处理CheckBox.CheckedChanged
事件:
protected void Check_Clicked(Object sender,EventArgs e) { // get the checkBox reference CheckBox chk = (CheckBox)sender; // get the GridViewRow reference GridViewRow row = (GridViewRow) chk.NamingContainer; // assuming the primary key value is stored in a hiddenfield with ID="HiddenID" HiddenField hiddenID = (HiddenField) row.FindControl("HiddenID"); string sql = "UPDATE dbo.Table SET Status=@Status WHERE idColumn=@ID"; using (var con = new sqlConnection(connectionString)) using (var updateCommand = new sqlCommand(sql,con)) { updateCommand.Parameters.AddWithValue("@ID",int.Parse(hiddenID.Value)); // assuming the type of the column is bit(boolean) updateCommand.Parameters.AddWithValue("@Status",chk.Checked); con.open(); int updated = updateCommand.ExecuteNonQuery(); } }
.net – 在gridview末尾插入复选框列
我尝试在.aspx文件中添加通常的模板字段(来自设计选项卡),但是添加了一个复选框作为第一列而不是最后一列.
解决方法
dataTable.Columns.Add("Select",Type.GetType("System.Boolean")); Demogrid.DataSource = dataTable; Demogrid.DataBind(); foreach (GridViewRow row in Demogrid.Rows) { //check Box is the first control on the last cell. CheckBox check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox; check.Enabled = true; }
请注意,您的asp:GridView实际上是AutoGenerated.
android – 在gridview复选框中取消选中,同时向上和向下滚动gridview
我有一个GridView,我已经放了一个复选框和imageview.
问题是,当我选中此复选框并向下滚动时,checBox将被取消选中.虽然uncheck事件未被触发,但它看起来未经检查.我没有在复选框中应用任何样式.可能是什么原因?我怎样才能解决这个问题.
holder.chckbx.setonCheckedchangelistener(new OnCheckedchangelistener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Log.e("checked","checked");
}
else{
Log.e("unchecked","unchecked");
}
}
});
解决方法:
https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M
在上面的链接中从romain guy的listview解决方案中提取.
您的自定义适配器必须实现CompoundButton.OnCheckedchangelistener并使用SparseBooleanArray
然后
holder.chckbx.setonCheckedchangelistener(this);
然后
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
例
public class MainActivity extends Activity implements
AdapterView.OnItemClickListener {
int count;
private CheckBoxAdapter mCheckBoxAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridView gridView = (GridView) findViewById(R.id.lv);
gridView.setTextFilterEnabled(true);
gridView.setonItemClickListener(this);
mCheckBoxAdapter = new CheckBoxAdapter(this);
gridView.setAdapter(mCheckBoxAdapter);
}
public void onItemClick(AdapterView parent, View view, int
position, long id) {
mCheckBoxAdapter.toggle(position);
}
class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedchangelistener
{ private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
ImageView imageview1,imageview;
CheckBox cb;
CheckBoxAdapter(MainActivity context)
{
super(context,0);
mCheckStates = new SparseBooleanArray(10);
mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// Todo Auto-generated method stub
return 10;
}
@Override
public Object getItem(int position) {
// Todo Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// Todo Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Todo Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.checkBox, null);
imageview= (ImageView) vi.findViewById(R.id.textView1);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setonCheckedchangelistener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// Todo Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}
activity_main.xml中
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:numColumns="2"
android:layout_height="wrap_content"
/>
</RelativeLayout>
checkBox.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="34dp"
android:src="@drawable/ic_launcher"/>
<CheckBox
android:id="@+id/checkBox1"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginRight="22dp"
android:layout_marginTop="23dp" />
</RelativeLayout>
类似于这里回答的那个.而不是listview使用gridview.
How to change the text of a CheckBox in listview?
asp.net gridview复选框选择
我有一个gridview,在gridview里面我有一个复选框,点击复选框后,我正在做一个回发事件,并尝试仅在数据库上更新这个特定的行.
这是我的gridview复选框代码.看到OnCheckedChanged.
<asp:TemplateField HeaderText="Sample"> <ItemTemplate> <asp:CheckBox runat="server" ID="chkSample" Checked='<%# Bind("Sample") %>' OnCheckedChanged="UpdateSupplyLed" AutopostBack="True"> </asp:CheckBox> </ItemTemplate> </asp:TemplateField>
码:
protected void UpdateSupplyLed(object sender,EventArgs e) { foreach (GridViewRow di in SamplingGridView.Rows) { CheckBox chkBx = (CheckBox)di.FindControl("chkSample"); if (chkBx != null && chkBx.Checked) { //update database logic here. } } }
上面的代码工作正常,但它给我的所有复选框都被检查,而不管我刚检查的那个.我不想要所有这些.
如何才能获得刚刚检查过的唯一一行值.某些行可能已经被检查过,因为这些记录的状态为true,我不想更新这些记录.
我想我的问题是对的!
更新:答案是:
protected void UpdateSupplyLed(object sender,EventArgs e) { CheckBox chkSampleStatus = sender as CheckBox; bool sample = chkSampleStatus.Checked; GridViewRow row = chkSampleStatus.NamingContainer as GridViewRow; TextBox txtId = row.FindControl("Id") as TextBox; int id = Int32.Parse(txtId.Text); }
解决方法
CheckBox chkBx = sender as CheckBox;
而不是迭代所有行.
我自己没有以这种方式在GridView中使用CheckBox.通常我会使用GridView的OnRowCommand事件,并使用RowIndex或CommandArgument值来更新数据库.
考虑一下OnRowcommand对于CheckBox来说可能很棘手,一个更好的解决方案可能是坚持使用复选框的CheckChanged事件并使用控件NamingContainer导航到GridViewRow服务器端.就像是:
GridViewRow row = chkBx.NamingContainer as GridViewRow;
我假设去了CheckBox => Cell =>如果您使用Google ASP.NET NamingContainer,您将获得更多细节.
asp.net – 从标签容器中的数据绑定gridview丢失更新面板触发器
<ajaxToolkit:TabContainer runat="server" ID="tabBody"> <ajaxToolkit:TabPanel runat="server" ID="tabPanel1"> <ContentTemplate> <asp:UpdatePanel runat="server" ID="updPanel1"> <ContentTemplate> <asp:Gridview runat="server" ID="Grd1" OnRowCommand="Grd1_RowCommand" OnRowDataBound="Grd1_RowDataBound"> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="lnkChangePanels" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="gotopanel2" Text='<%#Eval("FirstName") & " " & Eval("LastName")%>' /> </ItemTemplate> </asp:TemplateField> </asp:Gridview> </ContentTemplate> </asp:UpdatePanel> </ContentTemplate> </ajaxToolkit:TabPanel> <ajaxToolkit:TabPanel runat="server" ID="tabPanel2"> <ContentTemplate> <asp:UpdatePanel runat="server" ID="updPanel2"> <ContentTemplate> <asp:Gridview runat="server" ID="Grd2"> </asp:Gridview> </ContentTemplate> </asp:UpdatePanel> </ContentTemplate> </ajaxToolkit:TabPanel> </ajaxToolkit:TabContainer>
为了填充面板1上的gridview,有一个用户输入的搜索框,我调用一个函数将linq查询绑定到它.
现在我将rowcommand添加为rowdatabound上的回发触发器:
Protected Sub Grd1_RowDataBound(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DaTarow Then Dim lb As LinkButton = CType(e.Row.FindControl("lnkChangePanels"),LinkButton) If Not lb Is nothing Then ToolkitScriptManager1.RegisterPostBackControl(lb) End If End If End Sub
然后这是我必须触发选项卡面板更改(并做一些其他的东西)的代码:
Protected Sub Grd1_RowCommand(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Grd1.RowCommand Dim id = e.CommandArgument.ToString() Select Case e.CommandName Case "gotopanel2" eventDetails(id,"C") tabBody.ActiveTab = tabPanel2 End Select End Sub
这会导致正确的回发并更改选项卡,一切都按预期工作.但是如果我回到第一个标签并尝试点击gridview 1中的另一行,则没有任何反应.
有没有办法构建这个,以便选项卡可以更改而不会丢失回发触发器或我是否会发生这种错误?
谢谢.
解决方法
将整个TabContainer放在UpdatePanel中,您可以从选项卡中删除UpdatePanel(但您不必这样做).确保该新面板的UpdateMode设置为“始终”.
我认为在你的例子中它没有改变的原因是UpdatePanel只刷新它自己的内容和属性,决定是否可见tab是否为在UpdatePanel之外的div(tabPanel)设置.当您返回带有网格的选项卡时,您可以通过单击它来实现客户端,并且当它出错时.
要了解问题的根源并找出它在第一次回发期间的工作原理,您可能需要调试用于TabContainer控件的ajax工具包javascript.
今天的关于更新gridview asp.net中的复选框更改数据库c#和aspnet的gridview里数据修改的分享已经结束,谢谢您的关注,如果想了解更多关于.net – 在gridview末尾插入复选框列、android – 在gridview复选框中取消选中,同时向上和向下滚动gridview、asp.net gridview复选框选择、asp.net – 从标签容器中的数据绑定gridview丢失更新面板触发器的相关知识,请在本站进行查询。
本文标签: