Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table which contains userid,productid,permission . Userid and productid are inserted from a page and permission is getting false automatically since default value is false . Now i want to show the userid ,productid and permission in a grid view and if admin wants it will check and make true the permission .

I take a checkbox on the page for that and on rowdatabound when checkbox.checked ! = null then i inserted true and take that value in session . And then update in the table on the button click but checking the check box not working .
Posted
Comments
Muralikrishna8811 5-Oct-11 8:33am    
post your code

Hi,

Here I write some code for checking checkbox in data controls

ASP.NET
<asp:datalist id="DataList1" runat="server" width="70%" onitemcommand="DataList1_ItemCommand" xmlns:asp="#unknown">
          <HeaderTemplate>
              <table width="100%" align="center" border="1">
          </HeaderTemplate>
          <itemtemplate>
              <tr>
                  <td>
                      <asp:checkbox id="CheckBox1" runat="server" />
                  </td>
                  <td>
                      <asp:linkbutton id="LinkButton1" commandname="changepremi" commandargument="<%#Eval("Userid")" runat="server">change</asp:linkbutton>
                  </td>
              </tr>
          </itemtemplate>
          <footertemplate>
              </footertemplate></table>

      </asp:datalist>


And code behind file contains following code
C#
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "changepremi")
    {
        string id = e.CommandArgument.ToString();
        CheckBox chbox = (CheckBox)e.Item.FindControl("CheckBox1");
        if (chbox.Checked)
        {
            // update database
        }
        else
        {
            //update database
        }
    }
}



I hope my guess is right in your question .We cannot raise data control event with checkbox in data control except selectedchange event.


All the Best
 
Share this answer
 
Comments
Vimalrashamra 5-Oct-11 5:52am    
You are right sir, But i want that on the place of update database , the checkbox value will be true and then i store it in the session . And when user clicks on the submit button then it will be inserted . Because if user checked and then unchecked it its value is already inserted in the database
Muralikrishna8811 5-Oct-11 5:56am    
can you post your design code
Vimalrashamra 5-Oct-11 6:49am    
Thanks Murli for help . Its working fine now .
Vimalrashamra 5-Oct-11 6:54am    
but a problem still occurs when i check any checkbox its take all the checkbox as checked
Muralikrishna8811 5-Oct-11 6:57am    
check your data filling method must be in
if(!ispostback)
{
//fill data
}

use template field to add controls in grid
 
Share this answer
 
Comments
Vimalrashamra 5-Oct-11 6:43am    
doing same for check box and i dont want to change the adminid and itemid
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label runat="server" ID="Amt"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Use different type of control according to the requirment ...Fine
 
Share this answer
 
Comments
Vimalrashamra 5-Oct-11 6:48am    
Its working by BoundField also and actually its not showing data because i did not add DataField Property . Anyways Thanks
Hi,

check this once

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:datalist id="DataList1" runat="server" width="337px" xmlns:asp="#unknown">
            <HeaderTemplate>
                <table width="100%" align="center">
                    <tr>
                        <td>
                            User ID
                        </td>
                        <td>
                            Product ID
                        </td>
                        <td>
                            Permission
                        </td>
                    </tr>
            </HeaderTemplate>
            <itemtemplate>
                <tr>
                    <td>
                        <asp:label id="Lbluid" runat="server" text="<%#Eval("userid") %>"></asp:label>
                    </td>
                    <td>
                        <asp:label id="Lblpid" runat="server" text="<%#Eval("proid") %>"></asp:label>
                    </td>
                    <td>
                        <asp:checkbox id="CheckBox1" runat="server" />
                    </td>
                </tr>
            </itemtemplate>
            <footertemplate>
                </footertemplate></table>
            
        </asp:datalist>
        <br />
        <asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" xmlns:asp="#unknown" />
    </div>
    </form>
</body>
</html>


And code behind file contains

C#
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        filldata();
    }
}
public void filldata()
{

    dt.Columns.Add("userid");
    dt.Columns.Add("proid");
    addrowin("1", "123");
    addrowin("2", "324");
    addrowin("5", "67");
    addrowin("7", "87");
    addrowin("9", "23");
    DataList1.DataSource = dt;
    DataList1.DataBind();
}
public void addrowin(string uid,string pid)
{
    DataRow dr = dt.NewRow();
    dr[0] = uid;
    dr[1] = pid;
    dt.Rows.Add(dr);
}

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (DataListItem dti in DataList1.Items)
    {
        string qry="";
        string uid = ((Label)dti.FindControl("Lbluid")).Text;
        string pid = ((Label)dti.FindControl("Lblpid")).Text;
        CheckBox chper=(CheckBox )dti.FindControl ("CheckBox1");
        if(chper .Checked )
        {
            qry = "insert into PermissionTab values ("+uid +","+pid+",'True'";
        }
        else
        {
            qry = "insert into PermissionTab values (" + uid + "," + pid + ",'False'";
        }
    }
}


Now tell me is it correct or not

All the Best
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900